用python编个小程序

让用户想一个1到100以内的数字,然后程序猜用户所想的数,用户回答是否猜对,如果不对,用偏高或偏低才提示程序继续猜,直到猜对,返回猜的数字和猜的次数。赶着交作业,哪位大侠... 让用户想一个1到100以内的数字,然后程序猜用户所想的数,用户回答是否猜对,如果不对,用偏高或偏低才提示程序继续猜,直到猜对,返回猜的数字和猜的次数。
赶着交作业,哪位大侠来帮帮我~!!!最快的有加分!!!
展开
 我来答
7764sky
2010-09-18 · TA获得超过2616个赞
知道大有可为答主
回答量:1245
采纳率:0%
帮助的人:1394万
展开全部
#!/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
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式