FAQ
Hello,

I am teaching myself python using the book: Python Programming for Absolute Beginners, 2nd edition by Michael Dawson. I am using python 2.7.1.

In chapter 3 we are learning to use structures (while, if, elif) to write a program that has the user guess a number between 1 and 100.

Here is the code for the baseline program:

------------- start --------------

# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"

# set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1

# guessing loop
while (guess != the_number):
if (guess > the_number):
print "Lower..."
else:
print "Higher..."

guess = int(raw_input("Take a guess: "))
tries += 1

print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"

raw_input("\n\nPress the enter key to exit.")

------------------- end ---------------------

The book asks to write a version of this program that limits the number of guess the user can take. I have tried to write this program, and I am getting some run time errors. Can anybody take a look at my code and give me some advice or hints?

Thanks!

---- start ---

# Number Guessing Game Version 2
#
# The computer picks a random number between 1 and 100
# The player tries to guess and the computer tells
# the player if the guess is high or low or correct
# The player has to guess the number in less than 7 tries.
#
# 1/12/2011

import random

# welcome the player to the game

print "\tWelcome to 'Guess My Number'!"
print "\nI am thinking of a number between 1 and 100."
print "Try and guess it in as few attempts as possible.\n"

# Set the initial values

the_number= random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1

# Guessing loop

while guess != the_number:
while tries > 7:
if guess > the_number:
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take a guess: "))
tries += 1

print "You guessed it! The number was: ", the_number
print "And it only took you", tries, "tries!\n"

print "Wow, you suck at this, you should be able to solve this in 7 attempts or less"

raw_input("Press Enter to exit the program.")

------- end -----

## Search Discussions

• at Jan 12, 2011 at 7:41 pm ⇧ On 2011-01-12, Physics Python wrote:
while guess != the_number:
=================================================
while tries > 7:
if guess > the_number:
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take a guess: "))
tries += 1
=================================================

Think about what happens when this nested loop exits because tries > 7? It
returns to the outer loop whether or not the actual number was guessed
correctly. There is no real need for this loop.
print "You guessed it! The number was: ", the_number
print "And it only took you", tries, "tries!\n"
Note that the outer loop ends here without any test to see whether or not
the number was actually guested and there is *nothing* that stops this
outer loop, so it will spin forever.
print "Wow, you suck at this, you should be able to solve this in 7 attempts or less"

raw_input("Press Enter to exit the program.")
This is never reached.
• at Jan 12, 2011 at 7:52 pm ⇧ Thanks,

Is this an indentation problem then?
How do I update the sentinel within the secondary while loop. I am trying to avoid using breaks by the way, as I can program this example using breaks:

--- start---
import random
print "\tWelcome to 'Guess my number'!:"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"

the_number = random.randrange(1,101)

tries = 0

while True:
guess = int(raw_input("Take a guess: "))
tries += 1
if guess > the_number:
print "Lower..."
elif guess < the_number:
print "Higher..."
else:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
break
if tries == 7:
print "Wow you suck! It should only take at most 7 tries!"
break

raw_input ("\n\nPress the enter key to exit.")

--- end ---

But the book states that this can be done without needing to use breaks.

Thanks!
• at Jan 12, 2011 at 8:04 pm ⇧ Return False instead of break should work

else:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
return False

Jason

..?><((((?>
-----Original Message-----
From:
n.org] On Behalf Of Physics Python
Sent: Wednesday, January 12, 2011 2:53 PM
To: python-list at python.org
Subject: Re: Nested structures question

Thanks,

Is this an indentation problem then?
How do I update the sentinel within the secondary while loop.
I am trying to avoid using breaks by the way, as I can
program this example using breaks:

--- start---
import random
print "\tWelcome to 'Guess my number'!:"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"

the_number = random.randrange(1,101)

tries = 0

while True:
guess = int(raw_input("Take a guess: "))
tries += 1
if guess > the_number:
print "Lower..."
elif guess < the_number:
print "Higher..."
else:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
break
if tries == 7:
print "Wow you suck! It should only take at most 7 tries!"
break

raw_input ("\n\nPress the enter key to exit.")

--- end ---

But the book states that this can be done without needing to
use breaks.

Thanks!
--
http://mail.python.org/mailman/listinfo/python-list
• at Jan 12, 2011 at 8:12 pm ⇧ [wrapped lines to <80 characters per RFC 1855]
On 2011-01-12, Physics Python wrote:
Is this an indentation problem then?
That depends how you look at it. I was not clear from your code exactly
where you wanted to handle things.
How do I update the sentinel within the secondary while loop. I am
trying to avoid using breaks by the way, as I can program this example
using breaks:
You don't need breaks.
import random
print "\tWelcome to 'Guess my number'!:"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"

the_number = random.randrange(1,101)

tries = 0

while True:
while can be used to test for more then a single condition at a time using
and/or chains.
else:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
break
if tries == 7:
print "Wow you suck! It should only take at most 7 tries!"
break
Both of these tests can be performed as part of the loop itself. The end
results can therefore be tested and handled outside of the loop without
using breaks.
• at Jan 12, 2011 at 10:19 pm ⇧ In case you still need help:

- # Set the initial values
- the_number= random.randrange(100) + 1
- tries = 0
- guess = None
-
- # Guessing loop
- while guess != the_number and tries < 7:
- guess = int(raw_input("Take a guess: "))
- if guess > the_number:
- print "Lower..."
- elif guess < the_number:
- print "Higher..."
- tries += 1
-
- # did the user guess correctly to make too many guesses?
- if guess == the_number:
- print "You guessed it! The number was", the_number
- print "And it only took you", tries, "tries!\n"
- else:
- print "Wow you suck! It should only take at most 7 tries!"
-
- raw_input("Press Enter to exit the program.")
• at Jan 12, 2011 at 8:22 pm ⇧ On 2011-01-12, Jason Staudenmayer wrote:
Return False instead of break should work

else:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
return False
Since he isn't in a function, that isn't any good. He would import sys and
use sys.exit() but that rather defeats the purpose of having a single entry
and exit point to the loop.
• at Jan 13, 2011 at 6:57 am ⇧ looping = True
while looping:
guess = int(raw_input("Take a guess: "))
tries += 1
if guess > the_number:
print "Lower..."
elif guess < the_number:
print "Higher..."
else:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
break

if tries >= 7:
print "Wow you suck! It should only take at most 7 tries!"
looping = False

# Alternatively while learing while looping use the continue statement

looping = True
while looping:
guess = int(raw_input("Take a guess: "))
tries += 1

if guess > the_number:
print "Lower..."
elif guess < the_number:
print "Higher..."
else:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
break

if tries < 7:
continue

print "Wow you suck! It should only take at most 7 tries!"
looping = False

# In a while loop I recommend to NOT end loops using the
# conditional test of == but instead use >, <, >= or <= or !=.
# In a complex while loop the exit condition may be skipped
# by mistake and you'll loop forever.

# In while loops I get less bugs by putting the incrementor as
# the last statement in the while block;
# this helps follow precedence like range(7) is - zero to 6
# as well as index 0 in a list is the first item. However
# while index: where index == 0 will exit the loop before
# it even starts as 0 == False (0 is not False but equals False)

# Use the while loop for looping an undetermined number of
# iterations or conditional iterations.
# Use for loops for an explicid number of iterations.

for tries in range(7):
guess = int(raw_input("Take a guess: "))
if guess > the_number:
print "Lower..."
elif guess < the_number:
print "Higher..."
else:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
break
if tries >= 7:
print "Wow you suck! It should only take at most 7 tries!"

# I'm guessing the chapter's test is to see if you remember the for
loop.

# start using print() to get into a good habit for Python 3.0+

# I haven't seen the book but often one part of while that is
# left off in tutorials is the "else" statement.
while condition:
"block"
else:
"block"

# you can use else for when the condition never happens.
• at Jan 13, 2011 at 11:13 am ⇧ Physics Python wrote:
Hello,

I am teaching myself python using the book: Python Programming for Absolute Beginners, 2nd edition by Michael Dawson. I am using python 2.7.1.

In chapter 3 we are learning to use structures (while, if, elif) to write a program that has the user guess a number between 1 and 100.

Here is the code for the baseline program:
[snip]

here is an example of code using a for loop, which is always better than
a while loop, when applicable of course.
It uses the for... else... statement which is rather strange at first
glance but has some uses, it's always good to know it exists.

http://paste.pocoo.org/show/319931/

JM