Newbie help
bruno modulix
onurb at xiludom.gro
Mon Feb 14 05:02:01 EST 2005
Chad Everett wrote:
> Hey guys,
>
> Hope you can help me again with another problem. I am trying to learn
> Python on my own and need some help with the following.
>
> I am writing a program that lets has the pc pick a number and the user
> has five guess to get the number.
> 1. BUG: If the number is say 35 and I guess 41 the program tells me
> that I guessed the correct number and tells me I guessed 31.
>
> 2.When I do get the correct number I can not get the program to stop
> asking me for the number.
>
>
> Your help is greatly appreciated.
>
> Chad
>
> # Five Tries to Guess My Number
> #
> # The computer picks a random number between 1 and 100
> # The player gets Five tries to guess it and the computer lets
> # the player know if the guess is too high, too low
> # or right on the money
> #
> # Chad Everett 2/10/2005
>
> import random
>
> print "\tWelcome to 'Guess My Number'!"
> print "\nI'm thinking of a number between 1 and 100."
> print "You Only Have Five Guesses.\n"
>
> # set the initial values
> number = random.randrange(100) + 1
> guess = int(raw_input("Go Ahead and Take a guess: "))
> tries = 1
>
>
> # guessing loop
>
>
> while guess != number:
You actually need a second condition : tries >= 5
>
> if (guess > number):
> print "Guess Lower..."
> else:
> print "Guess Higher..."
The fact that guess is not strictly greater than number doesn't mean
it's strictly lower... You should handle 3 cases :
1/ guess > number
2/ guess < number
3/ guess == number
Here the 2/ and /3 are handled by the same branch...
> guess = int(raw_input("Take Another guess: "))
> tries += 1
> print "You guessed it! The number was", number
> print "And it only took you", tries, "tries!\n"
This will be executed whatever ! This code should be *outside* the loop.
(remember that in Python, whitespace is significative...)
(snip)
HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list