Python Task03:异常处理

Python Task03:异常处理

import random
 
num = random.randint(1,100)
guess_chances = 7
print('您只有7次猜数字的机会哦!')
 
for i in range(1,guess_chances + 1):
    print('这是第'+ str(i) + '次猜数字')
    guess = input('请输入数字:')
    if guess.isdigit():
       guess = int(guess)
       if guess < num:
           print('您输入的数字太小了,您还有' + str(guess_chances - i)+'次机会,请重新输入:')
       elif guess > num:
           print('您输入的数字太大了,您还有' + str(guess_chances - i)+'次机会,请重新输入:')
       elif guess == num:
           print('恭喜您猜对了')
           break
    elif guess == 'q':
        print('退出游戏!')
        break
    else:
        print('输入的内容必须为整数,请重新输入:')
while (guess_chances - i) == 0:
    print('您输入已经超过7次,游戏结束!')
    break
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页