[Tutor] Need a Solution!
ayyaz
ayyaz84 at gmail.com
Thu Jun 11 21:40:49 CEST 2009
Raj Medhekar wrote:
> This email contains another modified code that I tried working with but
> still doesn't do the job right, although performs closer to what i want.
> I have been having some difficulty modifying this program to where a
> player has limited number of guesses and if he fails to guess correctly
> within that number he is asked to "Try Again" The original program code
> and the code that I modified are given below. Thanks for your help.
>
> Sincerely,
> Raj
>
> The Original program code:
>
> # 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")
>
> My modifications that don't quite work right in the code:
>
> # Guess my number (Modified for Chapter 3 Challenge 3)
> #
> # 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
> # The player has only Five guesses in which to guess correctly
> # otherwise he loses and is asked to try again
> # and the game ends
>
> 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..."
> &nbs p;
> while tries < 5:
> if guess == the_number:
> print "You're right, the number is",the_number
> else:
> print "try again"
> guess = int(raw_input("Take a guess:"))
> tries += 1
>
>
>
> raw_input("\n\nPress the enter key to exit")
>
> Modified Code #2 (Better than the last one but still not right)
> # Guess my number (Modified for Chapter 3 Challenge 3)
> #
> # 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
> # The player has only Five guesses in which to guess correctly
> # otherwise he loses and is asked to try again
> # and the game ends
>
> 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 and tries <5:
> if guess > the_number:
> print "Lower..."
> else:
> print "Higher..."
>
> guess = int(raw_input("Take a guess:"))
> tries += 1
>
> if tri es > 5:
> guess == the_number
> print "You're right"
> else:
> print "You lose"
>
> raw_input("\n\nPress the enter key to exit")
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
Hello Raj,
Here is how I would modify the original program in the book.
import random
print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
max_tries = 10 ##### modification
print "Try to guess it in no more than", max_tries, "attempts.\n" #####
modification
# 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) and (tries < max_tries): ##### modification
if (guess > the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take a guess: "))
tries += 1
##### modification
if tries<max_tries:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
else:
print "You exceeded the maximum number of tries allowed."
print "The number was", the_number
raw_input("\n\nPress the enter key to exit.")
More information about the Tutor
mailing list