鱼C论坛

 找回密码
 立即注册
查看: 2575|回复: 14

[已解决]第五讲:猜数字提醒输入不合法

[复制链接]
发表于 2021-2-3 18:11:19 | 显示全部楼层 |阅读模式

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

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

x
这个怎么原来的代码里面让他进行下去啊??为啥我插进去不管输入啥都是不合法??

  1. #这是原代码
  2. import random
  3. times = 3
  4. secret = random.randint(1,10)
  5. print('------------------我爱鱼C工作室------------------')
  6. # 这里先给guess赋值(赋一个绝对不等于secret的值)
  7. guess = 0
  8. # print()默认是打印完字符串会自动添加一个换行符,end=" "参数告诉print()用空格代替换行
  9. # 嗯,小甲鱼觉得富有创意的你应该会尝试用 end="JJ"?
  10. print("不妨猜一下小甲鱼现在心里想的是哪个数字:")
  11. while (guess != secret) and (times > 0):
  12.     temp = input()
  13.     guess = int(temp)
  14.     times = times - 1 # 用户每输入一次,可用机会就-1
  15.     if guess == secret:
  16.         print("我草,你是小甲鱼心里的蛔虫吗?!")
  17.         print("哼,猜中了也没有奖励!")
  18.     else:
  19.         if guess > secret:
  20.             print("哥,大了大了~~~")
  21.         else:
  22.             print("嘿,小了,小了~~~")
  23.         if times > 0:
  24.             print("再试一次吧:")
  25.         else:
  26.             print("机会用光咯T_T")
  27. print("游戏结束,不玩啦^_^")
复制代码


  1. #这是要插入的
  2. temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
  3. # not操作符的作用是将布尔类型的结果翻转:即取反的意思,not True == Flase
  4. while not isinstance(temp, int):
  5.     print("抱歉,输入不合法,", end='')
  6.     temp = input("请输入一个整数:")
复制代码
最佳答案
2021-2-4 12:22:33
感觉你想插入的那个代码只是一个示例而已,怎么看都在这里用不上,input() 获取到的一定是一个 str 类型的,你后面再怎么用 isinstance(temp, int) 返回的都是 False

也就是说无论你输入的是什么,你都会是不合法的

这是我学这一块的时候写的

  1. import random

  2. print("---------- This is my first try -----------")
  3. number = random.randint(0, 19)   # get a random number from [0,19]
  4. times = 0   # how many chance the player already used
  5. chance = 5  # how many chance the player have
  6. while(times < chance):
  7.     temp = input("enter a number here!\n")
  8.     if temp.isdigit():
  9.         times += 1
  10.         guess = int(temp)
  11.         if(guess == number):
  12.             print("yes, you are right!\nCongradulations, you win!")
  13.             break
  14.         else:
  15.             if(times < chance):
  16.                 print("please try again!")
  17.                 if(guess < number):
  18.                     print("it is too small!")
  19.                 elif(guess > number):
  20.                     print("it is too big!")
  21.                 print("chance tried: " + str(times) + "/" + str(chance))
  22.             else:
  23.                 print("you run out of the chance!\ngame is over!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-3 18:18:32 | 显示全部楼层
因为 input 默认是 str 类型


所以 while 的判断那里 not False 就是 True

输入的东西都是 str 类型,不论你输入了什么

可以使用

while temp.isdigit(): 替代 while not isinstance(temp, int):
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-3 18:26:04 | 显示全部楼层
必须用给定的这个代码吗?能不能加别的?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-3 18:30:35 | 显示全部楼层
因为input()是提取用户输入,一直是字符串,temp是用户输入的内容,是字符串比如:
  1. >>>type(input())
  2. 3
  3. <class 'str'>
复制代码

(isinstance(temp, int))就返回false,使用temp.isdigit()判断
.isdigit()返回布尔类型,如果字符串只有数字构成就返回ture,否则返回false
比如:
  1. >>> a = '1'
  2. >>> a.isdigit()
  3. True
  4. >>>
复制代码
  1. >>> a = 'a112'
  2. >>> a.isdigit()
  3. False
  4. >>>
复制代码


所以这么改:


  1. #这是要插入的
  2. temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
  3. # not操作符的作用是将布尔类型的结果翻转:即取反的意思,not True == Flase
  4. while not temp.isdigit():
  5.     print("抱歉,输入不合法,", end='')
  6.     temp = input("请输入一个整数:")
复制代码

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

使用道具 举报

发表于 2021-2-3 19:08:30 | 显示全部楼层
本帖最后由 ncx0331 于 2021-2-4 10:16 编辑

这样

  1. #这是原代码
  2. import random
  3. times = 3
  4. secret = random.randint(1,10)
  5. print('------------------我爱鱼C工作室------------------')
  6. # 这里先给guess赋值(赋一个绝对不等于secret的值)
  7. guess = 0
  8. # print()默认是打印完字符串会自动添加一个换行符,end=" "参数告诉print()用空格代替换行
  9. # 嗯,小甲鱼觉得富有创意的你应该会尝试用 end="JJ"?
  10. #这是要插入的
  11. temp = int(input("不妨猜一下小甲鱼现在心里想的是哪个数字:"))
  12. # not操作符的作用是将布尔类型的结果翻转:即取反的意思,not True == Flase
  13. while not isinstance(temp, int):
  14.     print("抱歉,输入不合法,", end='')
  15.     temp = int(input("不妨猜一下小甲鱼现在心里想的是哪个数字:"))
  16. while (guess != secret) and (times > 0):
  17.     guess = int(temp)
  18.     times = times - 1 # 用户每输入一次,可用机会就-1
  19.     if guess == secret:
  20.         print("我草,你是小甲鱼心里的蛔虫吗?!")
  21.         print("哼,猜中了也没有奖励!")
  22.     else:
  23.         if guess > secret:
  24.             print("哥,大了大了~~~")
  25.         else:
  26.             print("嘿,小了,小了~~~")
  27.         if times > 0:
  28.             print("再试一次吧:")
  29.         else:
  30.             print("机会用光咯T_T")
  31. print("游戏结束,不玩啦^_^")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-4 08:46:19 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-2-4 10:08:53 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-4 10:11:39 | 显示全部楼层

大佬,你自己试过吗??
  1. temp = input("请输入一个整数:")"不妨猜一下小甲鱼现在心里想的是哪个数字:")
复制代码

这个压根用不了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-4 10:14:42 | 显示全部楼层

本来我是想让你用 isdigit() 直接就就解决了,看到大佬们都说了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-4 10:17:10 | 显示全部楼层
Daniel_Zhang 发表于 2021-2-3 18:18
因为 input 默认是 str 类型

while not isinstance(temp, int):
这个不能插进去??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-4 10:18:11 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-2-4 10:33:50 | 显示全部楼层
额,就那插入的是不是实现不了,小甲鱼的作业上只是一个扩展?!?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-4 10:41:55 | 显示全部楼层
javezhan 发表于 2021-2-3 18:30
因为input()是提取用户输入,一直是字符串,temp是用户输入的内容,是字符串比如:

(isinstance(temp,  ...

大佬,完整的代码给我来一份呗,用 while not temp.isdigit(): 的话应该插在哪合适??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-4 12:22:33 | 显示全部楼层    本楼为最佳答案   
感觉你想插入的那个代码只是一个示例而已,怎么看都在这里用不上,input() 获取到的一定是一个 str 类型的,你后面再怎么用 isinstance(temp, int) 返回的都是 False

也就是说无论你输入的是什么,你都会是不合法的

这是我学这一块的时候写的

  1. import random

  2. print("---------- This is my first try -----------")
  3. number = random.randint(0, 19)   # get a random number from [0,19]
  4. times = 0   # how many chance the player already used
  5. chance = 5  # how many chance the player have
  6. while(times < chance):
  7.     temp = input("enter a number here!\n")
  8.     if temp.isdigit():
  9.         times += 1
  10.         guess = int(temp)
  11.         if(guess == number):
  12.             print("yes, you are right!\nCongradulations, you win!")
  13.             break
  14.         else:
  15.             if(times < chance):
  16.                 print("please try again!")
  17.                 if(guess < number):
  18.                     print("it is too small!")
  19.                 elif(guess > number):
  20.                     print("it is too big!")
  21.                 print("chance tried: " + str(times) + "/" + str(chance))
  22.             else:
  23.                 print("you run out of the chance!\ngame is over!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2021-2-4 12:42:22 | 显示全部楼层
Daniel_Zhang 发表于 2021-2-4 12:22
感觉你想插入的那个代码只是一个示例而已,怎么看都在这里用不上,input() 获取到的一定是一个 str 类型的 ...

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-28 07:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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