用python编个小程序
发布网友
发布时间:2022-04-21 01:07
我来回答
共1个回答
热心网友
时间:2022-04-19 02:08
#!/usr/bin/env python
'guessNumber.py -- my first Python : guess number'
# import random function
from random import randint
# generate a random number
number = randint(0, 100)
# set default value
counter = 0
last_num = {
"min":0,
"max":100,
}
# loop
while True:
n = int(raw_input("Enter a number between %d to %d:" % (last_num['min'], last_num['max'])))
counter += 1
if not (last_num['min'] < n < last_num['max']):
print "Invalid number. Please try again."
# jump to next loop
continue
if n == number:
print '%d times! And the number is %d' % (counter,number)
raw_input('press anykey to exit')
# finish, jump out of the loop
break
elif n < number:
last_num['min'] = n
elif n > number:
last_num['max'] = n