python中循环结构完成数字游戏

img


救救孩子!

你题目的解答代码如下:

from random import randint
a = randint(1, 100)
while True:
    try:
        temp=input('请输入一个数字:')
        guess=int(temp)
    except:
        print('请输入正确的数字')
    else:
        if guess==a:
            print('猜对啦!')
            break
        elif guess>a:
            print('太大啦!')
        else:
            print('太小啦!')

如有帮助,望采纳!谢谢!

img

import random
a=random.randint(1,100)
while True:
    b=int(input('猜数字:'))
    if a==b:
        print('恭喜你,猜对了!')
        break
    elif a>b:
        print('大小了,请重猜')
    elif a<b:
        print('大大了,请重猜')

觉得有用的话采纳一下哈