[Tutor] Guess my number?
Rob Andrews
rob.andrews at gmail.com
Sun Aug 7 17:19:41 CEST 2005
Let's take a look....
I pasted your code into a new window and gave it a go. You used tabs
for indentation (or they got in there somewhere in the
email/copy/paste process), which confused my tiny mind, so I replaced
'em with spaces.
I noticed a few things. Here's my code:
#####################################
import random
import sys
number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1
while (guess != number):
if (tries > 10):
print "too many guesses"
sys.exit()
if (guess > number):
print "Lower...\n"
elif (guess < number):
print "Higher...\n"
# elif (tries > 10):
# print "You're stupid!"
else:
print "Error!"
guess = int(raw_input("Take a guess: "))
tries += 1
#####################################
I noticed you mis-spelled "tries" in one place, so I changed the spelling.
I also moved the test for the number of tries to a separate "if"
statement running before the "if" you had already included, so if the
user guesses too many times, it won't continue to run through the
guess-checking routine.
And I used sys.exit() to get out cleanly when the user exceeds the guess limit.
-Rob
On 8/7/05, . , <administrata at hotmail.com> wrote:
> I want the program to say "You're stupid!" When a player fails to guess in
> 10 tries.
> But, it's not working..
>
>
> import random
>
> number = random.randrange(100) + 1
> guess = int(raw_input("Take a guess: "))
> tries = 1
>
> while (guess != number):
> if (guess > number):
> print "Lower...\n"
> elif (guess < number):
> print "Higher...\n"
> elif (tires > 10):
> print "You're stupid!"
> else:
> print "Error!"
>
> guess = int(raw_input("Take a guess: "))
> tries += 1
>
> print "\nYou guess it! The number was", number
> print "And it only took you", tries, "tries!\n"
> raw_input("\n\nPress the enter key to exit.")
More information about the Tutor
mailing list