What's wrong with this code?

N

Nathan Pinno

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/
 
?

=?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=

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
 
R

Robert Kern

Nathan said:
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
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
T

Tim Roberts

Nathan Pinno said:
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"

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"
 
J

John Machin

Nathan said:
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
 
J

John Machin

John said:
Nathan Pinno wrote:


"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
 
N

Nathan Pinno

Hi all,

Sure I'm able to publish the code, only the previous version though. If
you want the first version; find Josh Cogliati, and ask him. It didn't have
a random function, though. I got the idea to improve it from my computer
course.

Here is the code:

#Plays the guessing game higher or lower
# (originally written by by Josh Cogliati, improved by Quique)

#This should actually be something that is semi random like the last
digits of the time or something else.
number = 78
guess = 0

while guess != number:
guess =input("Guess a number: ")

if guess > number:
print "Too high"

elif guess < number:
print "Too low"

print "Just right"

HTH,
Nathan Pinno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,228
Latest member
MikeMichal

Latest Threads

Top