鱼C论坛

 找回密码
 立即注册
查看: 1473|回复: 5

[已解决]新手求助 HLEP

[复制链接]
发表于 2021-8-29 21:34:51 | 显示全部楼层 |阅读模式

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

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

x
import random

times = 3
secret = random.randint(1,10)

print('------------------我爱鱼C工作室------------------')
guess = 0
print("不妨猜一下小甲鱼现在心里想的是哪个数字:", end=" ")

while (guess != secret) and (times > 0):
    temp = input()
   
    if temp.isdigit():
        guess = int(temp)
        if guess == secret:
            print("我草,你是小甲鱼心里的蛔虫吗?!")
            print("哼,猜中了也没有奖励!")
        else:
            if guess > secret:
                print("哥,大了大了~~~")
            else:
                print("嘿,小了,小了~~~")
            if times > 1:
                print("再试一次吧:", end='')
            else:
                print("机会用光咯T_T")
    else:
        print("抱歉,您的输入有误,请输入一个整数:", end='')

    times = times - 1 # 用户每输入一次,可用机会就-1

print("游戏结束,不玩啦^_^")




    if temp.isdigit():  求解这个作用是什么?视频里面也没有讲解到,是指如果输入的是非数字就会编程输入就会执行输入有误,请重新输入的指令的意思吗?

-------------------------------------------------------------------------------------
import random
secret=random.randint(1,10)
times=2
temp=input("我的数字")
num=int(temp)

while (num!=secret)and(times>0):
    if num==secret:
        print("牛逼")
    else:
        if num>secret:
            print("大了")
        else:
            print("小了")
        if times>0:
            print("还有机会")
        else:
            print("GG")
        temp=input("数字啊")
        num=int(temp)
        times=times-1
        
if num==secret:
    print("666")
else:
    print("输入有误,请重新输入")
print("游戏结束")
if (num!=secret) and (times==0):
      print("答案是",secret)


这个是我自己编写的   如果参考第一段代码输入的是非数字的就提示输入错误,请重新输入,在我自己编写的这段基础上要怎么修改??
最佳答案
2021-8-29 21:51:25
  1. import random
  2. secret=random.randint(1,10)
  3. times=3
  4. num = 0
  5. while (num!=secret)and(times>0):
  6.     temp=input("我的数字")
  7.     if temp.isdigit():
  8.         num=int(temp)
  9.         if num==secret:
  10.             print("牛逼")
  11.         else:
  12.             times=times-1
  13.             if num>secret:
  14.                 print("大了")
  15.             else:
  16.                 print("小了")
  17.             if times>0:
  18.                 print("还有机会")
  19.             else:
  20.                 print("机会用完啦")
  21.             
  22.     else:
  23.         print('输入有误,重新输入')
  24.         
  25. if num==secret:
  26.     print("666")
  27. else:
  28.     print('GG')
  29. print("游戏结束")
  30. if (num!=secret) and (times==0):
  31.       print("答案是",secret)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-29 21:51:25 | 显示全部楼层    本楼为最佳答案   
  1. import random
  2. secret=random.randint(1,10)
  3. times=3
  4. num = 0
  5. while (num!=secret)and(times>0):
  6.     temp=input("我的数字")
  7.     if temp.isdigit():
  8.         num=int(temp)
  9.         if num==secret:
  10.             print("牛逼")
  11.         else:
  12.             times=times-1
  13.             if num>secret:
  14.                 print("大了")
  15.             else:
  16.                 print("小了")
  17.             if times>0:
  18.                 print("还有机会")
  19.             else:
  20.                 print("机会用完啦")
  21.             
  22.     else:
  23.         print('输入有误,重新输入')
  24.         
  25. if num==secret:
  26.     print("666")
  27. else:
  28.     print('GG')
  29. print("游戏结束")
  30. if (num!=secret) and (times==0):
  31.       print("答案是",secret)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-29 22:12:22 | 显示全部楼层
if temp.isdigit():判断输入的是不是数字,是的话继续,不是直接跳出
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-30 09:41:44 | 显示全部楼层

如果一定要我第二段的基础上添加这个的话怎么弄
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-30 09:48:04 | 显示全部楼层
g476292825 发表于 2021-8-30 09:41
如果一定要我第二段的基础上添加这个的话怎么弄
  1. import random
  2. secret=random.randint(1,10)
  3. times=2
  4. while True:
  5.     temp=input("我的数字")
  6.     if temp.isdigit():
  7.         num=int(temp)
  8.         break
  9.     else:
  10.         print("输入有误,重新输入")

  11. while (num!=secret)and(times>0):
  12.     if num==secret:
  13.         print("牛逼")
  14.     else:
  15.         if num>secret:
  16.             print("大了")
  17.         else:
  18.             print("小了")
  19.         if times>0:
  20.             print("还有机会")
  21.         else:
  22.             print("GG")
  23.         while True:
  24.             temp=input("数字啊")
  25.             if temp.isdigit():
  26.                 num=int(temp)
  27.                 break
  28.             else:
  29.                 print("输入有误,重新输入")
  30.         times=times-1
  31.         
  32. if num==secret:
  33.     print("666")
  34. else:
  35.     print("输入有误,请重新输入")
  36. print("游戏结束")
  37. if (num!=secret) and (times==0):
  38.       print("答案是",secret)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-30 11:38:09 | 显示全部楼层
发个帖
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-19 09:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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