鱼C论坛

 找回密码
 立即注册
查看: 869|回复: 4

[已解决]在这个基础上,加一个如果输入的不是数字,则不扣除次数再次输入

[复制链接]
发表于 2020-4-16 10:50:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import random
sercet=random.randint(1,20)
guess=0
times=3
while guess!=sercet and times>0:
    temp=input('请输入一个数字:')
    guess=int(temp)
    times=times-1
    if guess==sercet:
        print('猜对了')
    else:
        if guess>sercet:
            print('太大了')
        else:
            print('太小了')
            if times>0:
                print('再试一次:')
            else:
                print('没有机会了')
print('结束了')
最佳答案
2020-4-16 10:55:44
  1. import random
  2. sercet=random.randint(1,20)
  3. guess=0
  4. times=3
  5. while guess!=sercet and times>0:
  6.     temp=input('请输入一个数字:')
  7.     if not temp.isdigit():
  8.         continue
  9.     guess=int(temp)
  10.     times=times-1
  11.     if guess==sercet:
  12.         print('猜对了')
  13.     else:
  14.         if guess>sercet:
  15.             print('太大了')
  16.         else:
  17.             print('太小了')
  18.             if times>0:
  19.                 print('再试一次:')
  20.             else:
  21.                 print('没有机会了')
  22. print('结束了')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-16 10:53:51 | 显示全部楼层

  1. import random

  2. secret = random.randint(1,10)
  3. print('------------------我爱鱼C工作室------------------')
  4. try:
  5.     temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")   
  6.     guess = int(temp)
  7. except (ValueError, EOFError, KeyboardInterrupt):
  8.     print('输入错误!')
  9.     guess = secret
  10. while guess != secret:
  11.     temp = input("哎呀,猜错了,请重新输入吧:")
  12.     guess = int(temp)
  13.     if guess == secret:
  14.         print("我草,你是小甲鱼心里的蛔虫吗?!")
  15.         print("哼,猜中了也没有奖励!")
  16.     else:
  17.         if guess > secret:
  18.             print("哥,大了大了~~~")
  19.         else:
  20.             print("嘿,小了,小了~~~")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-16 10:55:44 | 显示全部楼层    本楼为最佳答案   
  1. import random
  2. sercet=random.randint(1,20)
  3. guess=0
  4. times=3
  5. while guess!=sercet and times>0:
  6.     temp=input('请输入一个数字:')
  7.     if not temp.isdigit():
  8.         continue
  9.     guess=int(temp)
  10.     times=times-1
  11.     if guess==sercet:
  12.         print('猜对了')
  13.     else:
  14.         if guess>sercet:
  15.             print('太大了')
  16.         else:
  17.             print('太小了')
  18.             if times>0:
  19.                 print('再试一次:')
  20.             else:
  21.                 print('没有机会了')
  22. print('结束了')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-16 10:56:28 | 显示全部楼层
如果输入的不是数字,转换成int就会报错,因此可以用try,except语句,输入不是数字就continue,退出此次循环,进行下一次循环
  1. import random
  2. sercet=random.randint(1,20)
  3. guess=0
  4. times=3
  5. while guess!=sercet and times>0:
  6.     temp=input('请输入一个数字:')
  7.     try:
  8.         guess=int(temp)
  9.     except:
  10.         continue
  11.     times=times-1
  12.     if guess==sercet:
  13.         print('猜对了')
  14.     else:
  15.         if guess>sercet:
  16.             print('太大了')
  17.         else:
  18.             print('太小了')
  19.             if times>0:
  20.                 print('再试一次:')
  21.             else:
  22.                 print('没有机会了')
  23. print('结束了')
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
努力去见你 + 5 + 5 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-16 11:04:13 | 显示全部楼层
  1. import random
  2. sercet=random.randint(1,20)
  3. guess=0
  4. times=3
  5. while guess!=sercet and times>0:
  6.     temp=input('请输入一个数字:')
  7.     if not temp.isdigit():                   #使用isdigit()判断是否输入数字
  8.         print('输入错误,',end='')             #打印一个错误
  9.         continue                             #回到循环条件句,没有运行time-1,不扣减次数
  10.     guess=int(temp)
  11.     times=times-1
  12.     if guess==sercet:
  13.         print('猜对了')
  14.     else:
  15.         if guess>sercet:
  16.             print('太大了')
  17.         else:
  18.             print('太小了')
  19.             if times>0:
  20.                 print('再试一次:')
  21.             else:
  22.                 print('没有机会了')
  23. print('结束了')
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
努力去见你 + 5 + 5 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-16 09:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表