Python——猜数字游戏

题目要求

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

代码示例:

import random
trycount = 0
computer = random.randint(1,100)
while trycount <5:
    player = int(input('Num:'))
    if player > computer:
        print('too big')
        trycount += 1
    elif player < computer:
        print('too small')
        trycount += 1
    else:
        print('Congratulation,you are right!')
        break
else:
    print('failed!!! 5 chances has been used !')

运行结果

游戏失败

Num:1
too small
Num:2
too small
Num:3
too small
Num:4
too small
Num:5
too small
failed!!! 5 chances has been used !

游戏成功

Num:60
too small
Num:80
too big
Num:68
Congratulation,you are right!

版权声明:本文为qq_42006358原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。