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

NameError: name 'guess' is not defined

P: n/a
I am very new to both programming and Pyhton and while trying to do
some practice using A byte of python an Error pops up on the IDLE
shell. I am using windows XP. PLease see below.
while running:
guess = int(raw_input('Enter an integer : '))

if guess == number:
print 'Congratulations, you guessed it.'
running = False # this causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that.'
else:
print 'No, it is a little lower than that.'
else:
print 'The while loop is over.'
# Do anything else you want to do here

print 'Done'

After typing the above as the book says, I get the error NameError:
name 'guess' is not defined
What Am I doing wrong?
Mar 22 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
wi******@gmail.com schrieb:
I am very new to both programming and Pyhton and while trying to do
some practice using A byte of python an Error pops up on the IDLE
shell. I am using windows XP. PLease see below.
while running:
guess = int(raw_input('Enter an integer : '))

if guess == number:
print 'Congratulations, you guessed it.'
running = False # this causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that.'
else:
print 'No, it is a little lower than that.'
else:
print 'The while loop is over.'
# Do anything else you want to do here

print 'Done'

After typing the above as the book says, I get the error NameError:
name 'guess' is not defined
What Am I doing wrong?
You most certainly did NOT write the above - because in Python
whitespace is significant, and thus the above would result in a
syntax-error because you need to have things like

if condition:
something()

and not

if condition:
something()

So unless you post what you *really* entered (and make sure your
NG-client/mailclient doesn't eat leading whitespace), nobody will be
able to help you.

Diez
Mar 22 '08 #2

P: n/a
wi******@gmail.com wrote:
I am very new to both programming and Pyhton and while trying to do
some practice using A byte of python an Error pops up on the IDLE
shell. I am using windows XP. PLease see below.
while running:
guess = int(raw_input('Enter an integer : '))

if guess == number:
print 'Congratulations, you guessed it.'
running = False # this causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that.'
else:
print 'No, it is a little lower than that.'
else:
print 'The while loop is over.'
# Do anything else you want to do here

print 'Done'

After typing the above as the book says, I get the error NameError:
name 'guess' is not defined
What Am I doing wrong?
You are not describing the problem accurately. The above program would
not even start *running*. It would produce an
IndentationError: expected an indented block
on the second line. If you did manage to get the to get the program
entered with correct indentation, it would gave a NameError on the
variable named running not on guess.

So now: Tell us what really happened, and perhaps your question can be
answered.

Hint:

Each of the lines that end in a colon start a block of code which must
be indented.

Any variable used in a calculation must be given a value *before* the
calculation is carried out.

I think this might be what you were trying to do (untested):
number = 12345 # Defines the number variable before using

while True:
guess = int(raw_input('Enter an integer : '))

if guess == number:
print 'Congratulations, you guessed it.'
break # this causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that.'
else:
print 'No, it is a little lower than that.'

print 'The while loop is over.'
print 'Done'
Gary Herron

Mar 22 '08 #3

P: n/a
On Mar 22, 10:44 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
willk...@gmail.com schrieb:
I am very new to both programming and Pyhton and while trying to do
some practice using A byte of python an Error pops up on the IDLE
shell. I am using windows XP. PLease see below.
while running:
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.'
running = False # this causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that.'
else:
print 'No, it is a little lower than that.'
else:
print 'The while loop is over.'
# Do anything else you want to do here
print 'Done'
After typing the above as the book says, I get the error NameError:
name 'guess' is not defined
What Am I doing wrong?

You most certainly did NOT write the above - because in Python
whitespace is significant, and thus the above would result in a
syntax-error because you need to have things like

if condition:
something()

and not

if condition:
something()

So unless you post what you *really* entered (and make sure your
NG-client/mailclient doesn't eat leading whitespace), nobody will be
able to help you.
If your newsclient *does* mangle whitespace, you can post your code
like:

exec """
number = 12345 # Defines the number variable before using
while True:
$guess = int(raw_input('Enter an integer : '))
$if guess == number:
$$print 'Congratulations, you guessed it.'
$$break # this causes the while loop to stop
$elif guess < number:
$$print 'No, it is a little higher than that.'
$else:
$$print 'No, it is a little lower than that.'
print 'The while loop is over.'
print 'Done'
""".replace("$", "\t")
Mar 22 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.