python【while练习(猜数字游戏)】

猜数字游戏:
1.系统随机生成一个1~100的数字;
2.用户共有5次机会猜;
3.如果用户猜测数字大于系统给出的数字,打印"too big"
4.如果用户猜测数字小于系统给出的数字,打印"too small"
5.如果用户猜测的数字等于系统给出的数字,打印"恭喜中奖",
并退出循环

import random
i=0
while i<=2:
    player = int(input('请输入你要比较的数字:'))
    computer = random.randint(1, 100)
    print(computer)
    if (computer == player):
        print('恭喜中奖')
        break
    elif (computer > player):
        print('too small')
        print('你还有%d次机会'%(2-i))
    else:
        print('too big')
        print('你还有%d次机会' % (2 - i))
    i+=1
print('三次机会已经用光')

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43407305/article/details/86523827