Newb ??
bonono at gmail.com
bonono at gmail.com
Tue Nov 8 23:18:44 EST 2005
I would rather to do the loop as :
max_try = 5
for x in xrange(max_try):
g = int(raw_input(["Take a guess","Take another guess"][x != 0]))
if g == number:
print "bingo, hit it in %i tries" % x + 1
break
elif g < number: print "try higher"
else: print "try lower"
else: print "sorry, you lose, the number is %i" % number
Chad Everett wrote:
> import random
>
> print "\tWelcome to 'Guess my Number'!"
> print "\nI'm Thinking of a Number between 1 and 100."
> print "\nTry 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 another Guess:"))
> tries +=1
> if tries == 5:
> print "Sorry you lose."
> print "The Correct Number was ", the_number
>
>
> print "You guess correctly!!"
> print "The number was:", the_number
> print "You got it correct in", tries, "tries"
>
> raw_input("Press Enter to exit the program")
More information about the Python-list
mailing list