
用python编个小程序
让用户想一个1到100以内的数字,然后程序猜用户所想的数,用户回答是否猜对,如果不对,用偏高或偏低才提示程序继续猜,直到猜对,返回猜的数字和猜的次数。赶着交作业,哪位大侠...
让用户想一个1到100以内的数字,然后程序猜用户所想的数,用户回答是否猜对,如果不对,用偏高或偏低才提示程序继续猜,直到猜对,返回猜的数字和猜的次数。
赶着交作业,哪位大侠来帮帮我~!!!最快的有加分!!! 展开
赶着交作业,哪位大侠来帮帮我~!!!最快的有加分!!! 展开
1个回答
展开全部
#!/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
'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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询