Description The computer will pick a number between 1 and 100. (You can choose any high number you want.) The purpose of the game is to guess the number the computer picked in as few guesses as possible. Input The user will enter his or her guess until the correct number is guessed. Output The program will keep asking the user to guess until he or she gets the number correct. Then the program will print how many guesses were required. Sample session Time to play a guessing game. Enter a number between 1 and 100: 62 Too high. Try again: 32 Too low. Try again: 51 Too low. Try again: 56 Congratulations! You got it in 4 guesses. |
#! /usr/bin/env python
import random
print 'Time to play a guessing game.'
number = int(raw_input('Enter a number between 1 and 100:'))
if (number > 100 or number < 0 ):
print "Please inpute one integer between 1 and 100"
#random.random()
# Return the next random floating point number in the range [0.0, 1.0).
random_number=int(random.random()*100)
# print random_number
x=0
while 1 :
x = x + 1
if number > random_number :
number = int(raw_input('Too high. Try again:'))
elif number < random_number :
number = int(raw_input('Too low. Try again:'))
else:
print 'Congratulations! You got it in %d guesses.' %x
break
Still have some problem :
1.if I input nothing, just enter.There will have a problem.
0 意見:
張貼留言