Nested structures question

2011-01-12 Thread Physics Python
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 -
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested structures question

2011-01-12 Thread Physics Python
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