By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 440,092 Members | 1,546 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

# What's wrong with this code?

 P: n/a Hi all, What's wrong with the following code? It says there is name error, that random is not defined. How do I fix it? # Plays the guessing game higher or lower. # Originally written by Josh Cogliati, improved first by Quique, then by Nathan Pinno. print "Higher or Lower" print number = random.choice(range(100)) guess = 0 while guess != number: guess = input("Guess a number: ") if guess > number: print "Too high" guess = input("Guess a number: ") elif guess < number: print "Too low" guess = input("Guess a number: ") print "Just right" Thanks. Nathan Pinno http://www.npinnowebsite.ca/ -- ---------------------------------------------------------------- Posted via UsenetRevolution.com - Revolutionary Usenet ** HIGH RETENTION ** Specializing in Large Binaries Downloads ** http://www.UsenetRevolution.com Jul 19 '05 #1
Share this Question
5 Replies

 P: n/a Nathan Pinno a écrit : Hi all, What's wrong with the following code? It says there is name error, that random is not defined. How do I fix it? Add "import random" at the top of your file Cheers, SB # Plays the guessing game higher or lower. # Originally written by Josh Cogliati, improved first by Quique, then by Nathan Pinno. print "Higher or Lower" print number = random.choice(range(100)) guess = 0 while guess != number: guess = input("Guess a number: ") if guess > number: print "Too high" guess = input("Guess a number: ") elif guess < number: print "Too low" guess = input("Guess a number: ") print "Just right" Thanks. Nathan Pinno http://www.npinnowebsite.ca/ -- ---------------------------------------------------------------- Posted via UsenetRevolution.com - Revolutionary Usenet ** HIGH RETENTION ** Specializing in Large Binaries Downloads ** http://www.UsenetRevolution.com Jul 19 '05 #2

 P: n/a Nathan Pinno wrote: Hi all, What's wrong with the following code? It says there is name error, that random is not defined. How do I fix it? You need to import random. -- Robert Kern rk***@ucsd.edu "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter Jul 19 '05 #3

 P: n/a "Nathan Pinno" wrote: Hi all, What's wrong with the following code? It says there is name error, thatrandom is not defined. How do I fix it? # Plays the guessing game higher or lower. # Originally written by Josh Cogliati, improved first by Quique, then byNathan Pinno. print "Higher or Lower" print number = random.choice(range(100)) guess = 0 while guess != number: guess = input("Guess a number: ") if guess > number: print "Too high" guess = input("Guess a number: ") elif guess < number: print "Too low" guess = input("Guess a number: ") print "Just right" There is a problem with this, caused by having to repeat the same code in multiple places. Sa that the number is 50. You get to the first "input" statment, and you enter 30. It prints "Too low", and asks you to enter another number. You enter 40. The "while" expression is true, so it will loop again, and prompt you to enter ANOTHER number, without telling you whether it was high or low. Better to eliminate duplicated code: import random print "Higher or Lower" print number = random.choice(range(100)) while 1: guess = input("Guess a number: ") if guess == number: break elif guess > number: print "Too high" else: print "Too low" print "Just right" -- - Tim Roberts, ti**@probo.com Providenza & Boekelheide, Inc. Jul 19 '05 #4

 P: n/a Nathan Pinno wrote: Hi all, What's wrong with the following code? It says there is name error, that random is not defined. How do I fix it? Others have already answered that question. This posting is a pre-emptive strike to head off the next half-a-dozen questions. # Plays the guessing game higher or lower. # Originally written by Josh Cogliati, improved first by Quique, then by Nathan Pinno. Some of us are interested in the process by which great pieces of code arise, how they are meticulously honed and polished, which craftpersons contributed what ... is it possible for you to publish the earlier versions? print "Higher or Lower" print number = random.choice(range(100)) "number" will refer to one of: 0, 1, ......, 98, 99 guess = 0 so you'll get a strange result by using zero here; try -1 instead while guess != number: guess = input("Guess a number: ") "guess" will refer to a string e.g. "42" which will *not* compare equal to the integer 42. Also you should use raw_input, not input. so do this: guess = int(raw_input("Guess a number: ")) if guess > number: print "Too high" guess = input("Guess a number: ") This will cause your program to ask TWICE per trip around the loop. Lose it. elif guess < number: print "Too low" guess = input("Guess a number: ") .... and again. print "Just right" General advice: 1. Look on the Python web site (http://www.python.org) for an introduction to Python for non-programmers. 2. Join the Python tutor list. 3. In this news group, read a little more than the threads that you have started; this guessing game was discussed in a thread started by somebody calling themselves ChuckDubya on 29 June!! Homework?? HTH, John Jul 19 '05 #5

 P: n/a John Machin wrote: Nathan Pinno wrote: guess = input("Guess a number: ") "guess" will refer to a string e.g. "42" which will *not* compare equal to the integer 42. Also you should use raw_input, not input. so do this: guess = int(raw_input("Guess a number: ")) Ahem ... I'll redo that: You'd be better using raw_input, which *then* means you need to wrap int() around it, ending up with the same recommendation: guess = int(raw_input("Guess a number: ")) Cheers, John Jul 19 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.