Re: [Tutor] Beginner question
On 10 Aug 2013, at 04:30, eschneide...@comcast.net wrote: I've been learning python from the website 'inventwithpython.com', and I'm on a chapter that covers the following code: import random import time def displayIntro(): print('You are in a land full of dragons. In front of you,') print('you see two caves. In one cave, the dragon is friendly') print('and will share his treasure with you. The other dragon') print('is greedy and hungry, and will eat you on sight.') print() def chooseCave(): cave = '' while cave != '1' and cave != '2': print('Which cave will you go into? (1 or 2)') cave = input() return cave def checkCave(chosenCave): print('You approach the cave...') time.sleep(2) print('It is dark and spooky...') time.sleep(2) print('A large dragon jumps out in front of you! He opens his jaws and...') print() time.sleep(2) friendlyCave = random.randint(1, 2) if chosenCave == str(friendlyCave): print('Gives you his treasure!') else: print('Gobbles you down in one bite!') playAgain = 'yes' while playAgain == 'yes' or playAgain == 'y': displayIntro() caveNumber = chooseCave() checkCave(caveNumber) print('Do you want to play again? (yes or no)') playAgain = input() I'm confused about what the line 'checkCave(caveNumber)' does and how it works. I would appreciate any help with this Thank you, Eric Hi Eric, This line calls the method 'checkCave()'. This method takes the argument caveNumber (checkCave(caveNumber) , which is a string returned from user input generated in the method chooseCave(). In checkCave method, the value (caveNumber) is then used in a if/else statement to compare with a random integer (parsed to a string str(frindlyCave)) to determine whether you will be eaten or given treasure ;) Hope that was of some help. Ciaran ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner question
On 12 August 2013 02:14, Karim Liateni kliat...@gmail.com wrote: 5ÿt5ÿ6hhhyyyfrrtr eschneide...@comcast.net a écrit : I've been learning python from the website 'inventwithpython.com', and I'm on a chapter that covers the following code: Just a quick note - not on the algorithm itself. If you run that in some IDEs, such as Wing101, all the time.sleep()s will concatenate, and all the prints will then print at once with no delay ;') If that happens, run it from the command line or try a different IDE. Jim -- If you don't know it's impossible, it's easier to do. --Neil Gaiman The Process is not the Picture...Reality can only be proved to be weakly-objective. Strong objectivity is a myth. --Bernardo Kastrup You cannot use logic to justify logic, so logic itself has no basis other than faith. --Agrippa ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python Arduino
Dear All I have designed a small program to control an arduino through python socket server. The front end is the Javaquery. My HTML code is given below $(function() { $( #slider ).slider({ min: 0, max: 180, step: 10, slide: function( event, ui ) { $( #amount ).val( $ + ui.value ); } }); $( #amount ).val( $ + $( #slider ).slider( value ) ); }); My python websocket code is given below from twisted.internet import reactor print Using Twisted reactor, reactor.__class__ print from twisted.python import usage, log from twisted.protocols.basic import LineReceiver from twisted.internet.serialport import SerialPort from twisted.web.server import Site from twisted.web.static import File from autobahn.websocket import listenWS from autobahn.wamp import WampServerFactory, WampServerProtocol, exportRpc class Serial2WsOptions(usage.Options): optParameters = [ ['baudrate', 'b', 9600, 'Serial baudrate'], ['port', 'p', 26, 'Serial port to use'], ['webport', 'w', 8080, 'Web port to use for embedded Web server'], ['wsurl', 's', ws://localhost:9000, 'WebSocket port to use for embedded WebSocket server'] ] Can you please help me with the python code . I want the server to write string which will be sent to the arduino digital pin to control the servo. The arduino can have the following in its code if(!Serial.available()) { // convert String to int. int recievedVal = stringToInt(); //analogWrite(servoPin,recievedVal); recievedVal = map(recievedVal,0,179,0,179); webcam.write(recievedVal); delay(15); pwmComplete = false; } Python side coding help is required CLEANTECH SOLUTION www.cleantechsolution.in SAVE PAPER , SAVE EARTH ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner question
def checkCave(chosenCave): print('You approach the cave...') time.sleep(2) print('It is dark and spooky...') time.sleep(2) print('A large dragon jumps out in front of you! He opens his jaws and...') print() time.sleep(2) friendlyCave = random.randint(1, 2) if chosenCave == str(friendlyCave): print('Gives you his treasure!') else: print('Gobbles you down in one bite!') playAgain = 'yes' while playAgain == 'yes' or playAgain == 'y': displayIntro() caveNumber = chooseCave() checkCave(caveNumber) print('Do you want to play again? (yes or no)') playAgain = input() Hi, - Here we are passing the chosen integer (1 or 2) got from chooseCave() method to checkCave as arguement - When called in while loop inside checkCave the following happens; - The statements of approaching the cave and seeing the dragon are printed with a time interval of 2 secs between each - Randomly either 1 or 2 is generated by the randint() method of random module in python. - That randomly generated integer (1 0r 2) is compared with our integer input (1 or 2) - If they match dragon gives us gold. Or else - We will be eaten by dragon :) But in the code there is a flaw. input() will evaluate your user input. i.e. If you give an integer expression it will tell the answer. And when you provide a number it will take it as int type. See below. var = input() 1+2+3 var 6 var = input() 2 type(var) type 'int' var = input() s Traceback (most recent call last): File stdin, line 1, in module File string, line 1, in module NameError: name 's' is not defined var = input() So since the integer number is checked with string in Line 13, it will run into infinite loop. If you use raw_input() instead of input() you will be able to run the example. Regards, Krishnan On Mon, Aug 12, 2013 at 10:25 PM, Jim Mooney cybervigila...@gmail.comwrote: On 12 August 2013 02:14, Karim Liateni kliat...@gmail.com wrote: 5ÿt5ÿ6hhhyyyfrrtr eschneide...@comcast.net a écrit : I've been learning python from the website 'inventwithpython.com', and I'm on a chapter that covers the following code: Just a quick note - not on the algorithm itself. If you run that in some IDEs, such as Wing101, all the time.sleep()s will concatenate, and all the prints will then print at once with no delay ;') If that happens, run it from the command line or try a different IDE. Jim -- If you don't know it's impossible, it's easier to do. --Neil Gaiman The Process is not the Picture...Reality can only be proved to be weakly-objective. Strong objectivity is a myth. --Bernardo Kastrup You cannot use logic to justify logic, so logic itself has no basis other than faith. --Agrippa ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Python-Help] Instancing class issue
Hi Dino, On 2013-08-12 20:32, Dino Bektešević wrote: def __init__(self, **keys): from . import util self.keys=keys self.load() where I don't understand what **keys mean, I've only seen that as **kwargs meaning other key words and arguments in examples. The name of the variable doesn't matter, that's still what it does; you can think of it as encapsulating any other keyword arguments. def foo(bar, **kwargs): ... print(bar: %s % (bar,)) ... print(kwargs: %r % (kwargs,)) ... foo(bar, baz=qux, wibble=wobble) bar: bar kwargs: {'baz': 'qux', 'wibble': 'wobble'} When I try to instance Astrom class by: new=Astrom() I receive a following error: Sorry, no idea about this bit, I've never used sdsspy. est of luck sorting this out. Chris pgpR9GydIB2YX.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginner question
But in the code there is a flaw. input() will evaluate your user input. i.e. If you give an integer expression it will tell the answer. And when you provide a number it will take it as int type. See below. Hi, Ignore my above statements if using Python 3. Sorry my bad. Had a doubt and went to the site to see the version of python used. My statement above is correct only if run in Python 2 and not in 3. Regards, Krishnan On Mon, Aug 12, 2013 at 11:01 PM, Krishnan Shankar i.am.song...@gmail.comwrote: def checkCave(chosenCave): print('You approach the cave...') time.sleep(2) print('It is dark and spooky...') time.sleep(2) print('A large dragon jumps out in front of you! He opens his jaws and...') print() time.sleep(2) friendlyCave = random.randint(1, 2) if chosenCave == str(friendlyCave): print('Gives you his treasure!') else: print('Gobbles you down in one bite!') playAgain = 'yes' while playAgain == 'yes' or playAgain == 'y': displayIntro() caveNumber = chooseCave() checkCave(caveNumber) print('Do you want to play again? (yes or no)') playAgain = input() Hi, - Here we are passing the chosen integer (1 or 2) got from chooseCave() method to checkCave as arguement - When called in while loop inside checkCave the following happens; - The statements of approaching the cave and seeing the dragon are printed with a time interval of 2 secs between each - Randomly either 1 or 2 is generated by the randint() method of random module in python. - That randomly generated integer (1 0r 2) is compared with our integer input (1 or 2) - If they match dragon gives us gold. Or else - We will be eaten by dragon :) But in the code there is a flaw. input() will evaluate your user input. i.e. If you give an integer expression it will tell the answer. And when you provide a number it will take it as int type. See below. var = input() 1+2+3 var 6 var = input() 2 type(var) type 'int' var = input() s Traceback (most recent call last): File stdin, line 1, in module File string, line 1, in module NameError: name 's' is not defined var = input() So since the integer number is checked with string in Line 13, it will run into infinite loop. If you use raw_input() instead of input() you will be able to run the example. Regards, Krishnan On Mon, Aug 12, 2013 at 10:25 PM, Jim Mooney cybervigila...@gmail.comwrote: On 12 August 2013 02:14, Karim Liateni kliat...@gmail.com wrote: 5ÿt5ÿ6hhhyyyfrrtr eschneide...@comcast.net a écrit : I've been learning python from the website 'inventwithpython.com', and I'm on a chapter that covers the following code: Just a quick note - not on the algorithm itself. If you run that in some IDEs, such as Wing101, all the time.sleep()s will concatenate, and all the prints will then print at once with no delay ;') If that happens, run it from the command line or try a different IDE. Jim -- If you don't know it's impossible, it's easier to do. --Neil Gaiman The Process is not the Picture...Reality can only be proved to be weakly-objective. Strong objectivity is a myth. --Bernardo Kastrup You cannot use logic to justify logic, so logic itself has no basis other than faith. --Agrippa ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python Programming for the Absolute Beginner - Chap 7 Q: 2
Hello, I am a python newbie. I am reading this book (Python Programming for the Absolute Beginner). I am on Chapter 7, Question 2. Improve the Trivia Challenge game so that it maintains a high-scores list in a file. The program should record the player's name and score. Store the high scores using a pickled object. I have the following code so far: def high_score(): Records a player's score high_scores = [] #add a score // Do current stuff for adding a new score... name = input(What is your name? ) player_score = int(input(What is your score? )) entry = (name, player_score) high_scores.append(entry) high_scores.sort(reverse=True) high_scores = high_scores[:5] # keep only top five # dump scores f = open(pickles1.dat, wb) pickle.dump(high_scores, f) f.close() f = open(pickles1.dat, rb) high_scores = pickle.load(f) print(high_scores) f.close() When I execute this program in the main() program I get only the existing single name, player_score list combination stored in the pickles1.dat file. Can someone walk me through how it can store all the values each time the program is ran? Thanks, Zack H. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Instancing class issue
Hello, I have an unusual problem when I try to instance a class Astrom from a set of tools for astronomy SDSSPY. Here's the link to the project page: http://code.google.com/p/sdsspy/ and this is the class I'm having problems instancing: http://code.google.com/p/sdsspy/source/browse/sdsspy/astrom.py I'm running LucidLynx (You are using Ubuntu 10.04 LTS released in April 2010 and supported until April 2013.) on a Oracle VirtualBox v4.2.12 and I'm using Python 2.6.5. (reason being that I'm also importing various other modules like esutil, scipy, numpy of which some don't work properly on never version, or so I have been informed). Home OS is win7 64bit. I'm not new to programming but don't consider me an expert, or even amateur in python, most about python I learned from diveintopython and other random sources I used as I got further into it solving problems. Ok now onto the problem itself: There's a method pix2eq in the class Astrom I wish to use. However because it's inside a class obviously I need to instance it because you can only use methods on class instances. When I read the Astrom class definition there clearly states: def __init__(self, **keys): from . import util self.keys=keys self.load() where I don't understand what **keys mean, I've only seen that as **kwargs meaning other key words and arguments in examples. When I try to instance Astrom class by: new=Astrom() I receive a following error: Traceback (most recent call last): File pyshell#7, line 1, in module novi=Astrom() File /usr/local/lib/python2.6/dist-packages/sdsspy/astrom.py, line 44, in __init__ self.load() File /usr/local/lib/python2.6/dist-packages/sdsspy/astrom.py, line 266, in load raise ValueError(send run= and camcol=) ValueError: send run= and camcol= which seems pretty straight forward except that I don't see it requiring me to do so anywhere and that when I try to follow instructions: new = Astrom(run=2888, camcol=1) (both run 002888 and camcol 1 exist I have the images I need downloaded on my drive already) I recieve error about my enviroment: Traceback (most recent call last): File pyshell#11, line 1, in module novi = Astrom (run=2888, camcol=1) File /usr/local/lib/python2.6/dist-packages/sdsspy/astrom.py, line 44, in __init__ self.load() File /usr/local/lib/python2.6/dist-packages/sdsspy/astrom.py, line 270, in load fname=files.filename('photoField', **keys) File /usr/local/lib/python2.6/dist-packages/sdsspy/files.py, line 274, in filename return fs.filename(ftype, run, camcol, field, **keys) File /usr/local/lib/python2.6/dist-packages/sdsspy/files.py, line 479, in filename f = expand_sdssvars(f, run=run, camcol=camcol, field=field, **keys) File /usr/local/lib/python2.6/dist-packages/sdsspy/files.py, line 870, in expand_sdssvars raise ValueError(err) ValueError: There were unexpanded variables: '$PHOTO_REDUX/runList.par', there may be no such run but you might try sending the rerun except search doesn't pop-out any runList.par files anywhere. Is it something I'm doing wrong or is it something I should contact the author Mr. Sheldon personally? Any comment about the class is appreciated, including the Astrom(object) type inheritance. Help is greatly appreciated, Dino Bektešević ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Programming for the Absolute Beginner - Chap 7 Q: 2
On 12/08/13 01:52, Zack Hasanov wrote: I have the following code so far: def high_score(): high_scores = [] name = input(What is your name? ) player_score = int(input(What is your score? )) entry = (name, player_score) high_scores.append(entry) high_scores.sort(reverse=True) high_scores = high_scores[:5] # keep only top five Note that you never read the stored values from your pickle file you just add a single value. # dump scores f = open(pickles1.dat, wb) pickle.dump(high_scores, f) f.close() This then dumps that single value to the file overwriting anything that might have already been there. f = open(pickles1.dat, rb) high_scores = pickle.load(f) print(high_scores) You then load the new value and print it. When I execute this program in the main() program I get only the existing single name, player_score list combination stored in the pickles1.dat file. I have no idea how you call this. Presumably from within a loop? But because you never read in the stored values before writing the new you only ever get the last value stored. You need to insert code like your last block above at the top of the function to pre-populate high_scores before adding the new entry. Once you get that working we can look at tidying up some of the unnecessary code that will be left over, but lets get it working first! HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3?
Hi Alan, Thanks, very much, for your time answering my question. I ended up using global variables for all the attributes I had to share between the MainWindow class and the thread, and it worked. I am sure there are better alternatives, but since it was just a couple of attributes, the code looks ok. Some responses in line at [SM] On Sun, Aug 11, 2013 at 12:05 PM, Alan Gauld alan.ga...@btinternet.comwrote: On 09/08/13 16:50, SM wrote: Sorry I only just picked this up. (ex: self.tab_fw = QtGui.QWidget(), self.tab_ann = QtGui.QWidget(), etc), its own textEdit window and its own progress bar widget. All the tabs are defined within this single class - they are not instances of the class, as the tabs are distinct from each other. All of that is working really well. OK, I'll assume the structure looks like MainGUIWindow - Tab1 - TextEdit1 - ProgressBar1 - any other widgets on tab1 - Tab2 - TextEdit2 - ProgressBar2 - any other widgets on tab2 - Tab3 - TextEdit3 - ProgressBar3 - any other widgets on tab3 [SM] Yes And that both the TextEdit and progress bar widgets are part of the Qt framework/library? So there are 3 distinct progress bar instances, one per tab? [SM]: Yes, that is right. an added feature, I am now asked to have a progress bar widget which appears when an operation is going on, to tell the user that he/she has to wait. Is this an extra (4th?) progress bar or is this the ones already described as being in the tabs? [SM] The latter. There are just 3 tabs and 3 progress bars Also, when you say appears do you mean its invisible until the operation starts? Or is it visible but inactive (maybe greyed out?) [SM] Wrong choice of word on my part. I should have said, widget which swings horizontally [SM]: Yes. I shouldn't say duplicated, but each tab has a different progress bar of its own. In the example, I was quoting the code for just on tab. The others will be implemented similarly. OK So I'm again going to assume: A total of 1 progress bar per tab. The added feature is what these bars are addressing? [SM]: yes So I am running two threads - first one is the progressbar widget which has a rectangular slab spinning sideways to indicate that the operation is going on. I usually expect a progress bar to indicate the percentage complete but this sounds like its just a spinning eggshell activity indicator. Is that correct? You have no progress value indication? [SM] Yes, it is more of a egg-whatever It just spins horizontally as opposed to showing the percentage of work done. That is part of the requirement. The second thread is the one which is actually doing the operation. I have a flag that the second thread sets after the job is done OK, again I'll assume we are dealing with just an activity indicator not a value display. [SM] yes and stops spinning and gets out of the loop when the flag is set. I have that working as well. OK, Good the picture is building. [SM]: I did start out with a timer. But due to my lack of experience with usage of timer in Python, the code I wrote was running sequentially That sounds like you tried to use a Python native timer. Normally in a GUI the toolkit (Qt in this case) provides a timer event that triggers via the normal event mechanism. You set the timer, the event arrives and you do something then reset the timer for the next time round. I found this page that describes the Qt approach (which includes multi-shot timers) and links to more detailed examples etc. http://qt-project.org/doc/qt-**4.7/timers.html#id-fd46b213-** 376e-4636-a7d2-8ae899de1e11http://qt-project.org/doc/qt-4.7/timers.html#id-fd46b213-376e-4636-a7d2-8ae899de1e11 [SM] Thanks, very much, for the link. I will go over it. In my opinion, if the timer works, using it is less complicated than using threads. Thanks! -SM The code is in C++ but should translate easily to python and PyQt. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ __**_ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor