python编写猜年龄的代码

python编写猜年龄的代码

import random #把random模块调用出来
age = random.randint(10,25)#用rand.randint制造一个随机数表
count = 0
while count < 3:#共有三次机会
    n = int(input('Guess the age:'))#输入用户通过键盘敲打的数据
    if n > age:
        print('Try smaller,你还有%s次机会'%(2-count))
        count += 1
    elif n < age:
        print('Try bigger,你还有%s次机会'%(2 - count))
        count += 1
    else:
        print('Yes,you get it')
        break#结束循环
print(age)

这个猜年龄的代码应该是学python最基础的,初学者可以参考


分享到 :
相关推荐

Leave a Reply

Your email address will not be published. Required fields are marked *