[Tutor] Random Number Generator
Hello All, I'm a bare beginner to python (or indeed) any programming. I'm helping myself become more proficient by making a text adventure game. The problem is I need a function (or module) that will generate a random number within a range, say 1-20 for example. The ability to program this is beyond my meager abilities at the moment but I'd really like to get started. Would anyone out there be willing to provide me the code for me? Or at least point me in the right direction for finding it myself? TYIA :-) - Be a better pen pal. Text or chat with friends inside Yahoo! Mail. See how.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Thanks (was Random Number Generator)
Thank you everyone for your help! I have no idea why it never occured to me to Google it. Thanks for the code. Now let's see if I can get this sucker to work! - Looking for last minute shopping deals? Find them fast with Yahoo! Search.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] While Loops and Modules
Hello again to all the wonderfully helpful folks on this list. Today I did my Google homework and I found this neat bit of code for a countdown timer. import time import threading class Timer(threading.Thread): def __init__(self, seconds): self.runTime = seconds threading.Thread.__init__(self) def run(self): time.sleep(self.runTime) print "Buzzz!!! Time's up!" t = Timer(30) t.start() I don't understand large chunks of it (don't know what threading, self, or __init__ mean) but that's not important at the moment. It works and I will learn the vocab eventually. I also wrote this bit of code for a math challenge which comes in the next part of my game. import random startNum = random.choice(range(1, 9)) newNum = startNum + 7 score = 0 print 'Start with the number ', startNum, '. Then continuously add 7 to that number until the timer runs out. You have 30 seconds.' answer = int(raw_input('Enter your answer: ')) if newNum == answer: print 'That is correct! Keep going.' score = score + 5 print 'Your score is ', score else: print 'That is incorrect. Please try again.' I understand this part just fine 'cause I actually wrote it myself. What I need to do now is put these two parts together so that the player will keep adding 7 to the starting number for 30 seconds then the loop breaks. I know I need a loop of some sort and I'm guessing it's a 'while' sort of thing. I couldn't find what I was looking for when I Googled. I'm not even sure I knew the right search terms. Does anyone know how I'd combine these two modules to make it work? - Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Still Can't Find Timed While Loops
Hello all, I now have my bit of code in while loop form and it works! It's great but not exactly what I wanted to do. I've been googling my heart out and I find lots of info on while loops and lots of info on timers that will execute an action AFTER a given interval but nothing on a timer that will execute an action DURING a given interval. What I'd really like to do at this point in my game is have the player execute the loop for 30 seconds then have it print the final score and break. Does anyone out there have any code that'll do that? Here's what I've got. I'm sure it ain't pretty and I'm happy to hear suggestions on cleaning it up as well. I know it needs some def's in there and possibly a class too. import random startNum = random.choice(range(1, 9)) print 'Start with the number ', startNum,'. Then continuously add 7 to that number until the timer runs out. You have 30 seconds.' score = 0 answer = int(raw_input('Enter your answer: ')) while (score < 50): startNum = startNum + 7 if startNum == answer: print 'That is correct!' score = score + 5 print 'Your score is ', score answer = int(raw_input('Enter your answer: ')) else: print 'That is incorrect. Game over. Your final score is ', score break else: print 'You win! Your final score is ', score - Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Timed While Loops Thank You
Thank you all so much for your patience and help! I haven't had a chance to try any of the code yet (gotta go be a mommy first) :^) I look forward to sitting down and playing around with it this evening though! I also finally found a book that seems to fit my learning style well. Hopefully it'll answer some of my more basic questions so I can save the tough stuff for the list. Thanks again to everyone. Have a great weekend! Please visit our website www.earlylightpublishing.com - Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How is "tuple" pronounced?
Is it tuh-ple (rhymes with supple) or is it two-ple (rhymes with nothing that I can think of)? Please visit our website www.earlylightpublishing.com - Never miss a thing. Make Yahoo your homepage.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How is "tuple" pronounced?
So it looks like most folks here and on the web are saying too-ple (rhymes with scruple or pupil... sorta). That's the one I'll go with... now that I can say it it's time to get back to learning how to use em! Please visit our website www.earlylightpublishing.com - Looking for last minute shopping deals? Find them fast with Yahoo! Search.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Versions
I have the latest python version too when I first started "A Byte of Python" my code wouldn't work either. My problem was that I was programming in the shell (the screen with the three '>>>' on it). I found when I wrote the examples in a new window (cntrl+N) they all worked as advertized. He never mentions that you need to use an 'editing window' (I think it's called). I just stumbled across the fact. - Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Versions
Actually the first thing I noticed is the author would say something like "you can run the program by pressing F5 or selecting "run program" from the "run" menu. Neither of those things work from the shell. Saving programs also didn't work well for me when I put them in the shell. Do people really write whole applications just using the shell? "earlylight publishing" <[EMAIL PROTECTED]> wrote >I have the latest python version too when I first started "A Byte of >Python" > my code wouldn't work either. My problem was that I was programming > in the shell (the screen with the three '>>>' on it). I found when > I wrote > the examples in a new window (cntrl+N) they all worked as > advertized. Unless you are doing some strange GUI type things it should work at the >>> prompt too. (Sometimes you need an extra line - that could fox beginners I suspect, if his examples contain multiple statements) > He never mentions that you need to use an 'editing window' > (I think it's called). I just stumbled across the fact. You shouldn't need to use an editing window, but the >>> prompt does execute each line 9or block) in turn thus: x=0 for n in range(5): x = n+x print x looks like this in IDLE: >>> x=0 >>> for n in range(5): x = n+x <--Extra line here! >>> print x 10 >>> Is that the kind of thing you are seeing? - Never miss a thing. Make Yahoo your homepage.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Versions
I'm a "she" not a "he". :-) But actually I don't believe I was a member of this group when I was working with the book "A Byte Of Python" I don't believe I ever described a problem with raw_input here. That concept seems pretty clear to me but as you say the OP hasn't described a specific problem. As I said before, it was the fact that the author was describing features that I was not seeing in the shell that prompted me to try to figure out the "new window" feature.As soon as I solved the shell problem I had no further difficulties understanding the concepts in the book. I just thought I'd share what worked for me. :-) The OP has not specified what his problems specifically are, but "earlylight publishing" described his problem before, and he was not understanding why the >>> prompt was expecting immediate keyboard input when he typed in raw_input(). So a noob cannot figure out why it is advantageous to have a raw_input function that immediately asks for input. He thinks, "why can't I put the input in directly?" That is why putting a program into an edit window is very advantageous. I believe it is very likely that raw_input() is the culprit of confusion here. - Never miss a thing. Make Yahoo your homepage.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] upper and lower case input for file name
I don't know if this'll help or not but I just learned about this: file = raw_input(info).lower The .lower is supposed to convert any input to lower case. -- Message: 4 Date: Fri, 14 Dec 2007 11:14:13 -0500 From: "Bryan Fodness" <[EMAIL PROTECTED]> Subject: [Tutor] upper and lower case input for file name To: tutor-python Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset="iso-8859-1" Is there an easy way that an input can be upper or lower case? The file name is TEST.TXT, and I get. - Enter File (if not local, enter path):test.txt Traceback (most recent call last): File "test.py", line 52, in for line in open(file) : IOError: [Errno 2] No such file or directory: 'test.txt' - This is a non-issue on Windows, but now I have migrated to Ubuntu. Bryan - Looking for last minute shopping deals? Find them fast with Yahoo! Search.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python Versions
No prob about the gender confusion. :-) I'd be willing to bet most folks around here are male so it's not unreasonable to assume. I wasn't offended, just thought I'd share in the interest of accuracy. Thanks for the kind appology anyway. Hope I haven't set off a firestorm! Message: 1 Date: Fri, 14 Dec 2007 07:43:42 -0500 From: "Tiger12506" <[EMAIL PROTECTED]> Subject: To: Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original My apologies for mistaking your gender. Because English does not have adequate neutral gender indication, I tend to use the male as such, as they do in Spanish, and perhaps many other languages. At any rate, that's how it's written in the Bible. I presumed that it was an issue with raw input because not many other things are truly different within the prompt. An extra line is necessary within the prompt after blocks of code such as classes and function declarations. (I guess this didn't bother me when I first started learning python because I got irritated when I hit enter and it didn't do anything, so I hit enter again, much more violently. ;-) My apologies also to you for assuming what was the issue. I have a knack for knowing just what goes wrong, but there are many occasions where I am wrong. :-) - Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Bound To Be A Typo
Okay I copied this code directly from a book (author Michael Dawson) and it's not working. I'm sure I've missed something obvious like the spacing or something but I've been staring at it for 10 minutes and I can't see it. I'll put the code and error message below. Can someone else spot the problem? class Critter(object): """A virtual pet""" def ___init___(self, name): print "A new critter has been born!" self.name = name def __str__(self): rep = "Critter object\n" rep += "name: " + self.name + "\n" return rep def talk(self): print "Hi, I'm", self.name, "\n" #main crit1 = Critter("Poochie") crit1.talk() Here's the error message: Traceback (most recent call last): File "C:/Python25/attributecrit.py", line 15, in crit1 = Critter("Poochie") TypeError: default __new__ takes no parameters - Looking for last minute shopping deals? Find them fast with Yahoo! Search.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Bound To Be A Typo Thank You
I have no idea why I did that either. I know perfectly well it's supposed to be 2 underscores! Thanks to everyone who spotted the problem. "Michael H. Goldwasser" <[EMAIL PROTECTED]> wrote: You've inadvertently used three underscores around __init__ rather than two, and therefore you are not really defining __init__ but instead are relying upon the inherited one from object (which takes no parameters). With regard, Michael On Monday December 17, 2007, earlylight publishing wrote: > Okay I copied this code directly from a book (author Michael Dawson) and it's > not working. I'm sure I've missed something obvious like the spacing or > something but I've been staring at it for 10 minutes and I can't see it. I'll > put the code and error message below. Can someone else spot the problem? > > class Critter(object): > """A virtual pet""" > def ___init___(self, name): > print "A new critter has been born!" > self.name = name > > def __str__(self): > rep = "Critter object\n" > rep += "name: " + self.name + "\n" > return rep > > def talk(self): > print "Hi, I'm", self.name, "\n" > #main > crit1 = Critter("Poochie") > crit1.talk() > > Here's the error message: > > Traceback (most recent call last): > File "C:/Python25/attributecrit.py", line 15, in > crit1 = Critter("Poochie") > TypeError: default __new__ takes no parameters - Never miss a thing. Make Yahoo your homepage.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Why Won't My Pizza Fall?
Hey There Everyone, I'm following an example in a book and I can't find the error that's preventing this program from running. It's just an example of how to get a sprite moving. The images are all in the right folder. I can run the program and get a stationary sprite to appear. The trouble seems to come up when I add "dx" and "dy" Here's the code. from livewires import games SCREEN_WIDTH = 640 SCREEN_HEIGHT = 480 class Pizza(games.Sprite): """A falling pizza.""" def __init__(self, screen, x, y, image, dx, dy): """Initialize pizza object.""" self.init_sprite(screen = screen, x = x, y = y, image = image, dx = dx, dy = dy) #main my_screen = games.Screen(SCREEN_WIDTH, SCREEN_HEIGHT) wall_image = games.load_image("skywall.png", transparent = False) my_screen.set_background(wall_image) pizza_image = games.load_image("pizza.png") Pizza(screen = my_screen, x = SCREEN_WIDTH/2, y = SCREEN_HEIGHT/2, image = pizza_image, dx = 0, dy = 1) my_screen.mainloop() ___ Here's the error message: Traceback (most recent call last): File "C:/Python25/Chapter 11/movingsprite.py", line 25, in image = pizza_image, dx = 0, dy =1) File "C:/Python25/Chapter 11/movingsprite.py", line 15, in __init__ dx = dx, dy = dy) TypeError: init_sprite() got an unexpected keyword argument 'dx' Thanks in advance for any help you can give me. :-) - Never miss a thing. Make Yahoo your homepage.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor