鱼C论坛

 找回密码
 立即注册
查看: 1017|回复: 8

[已解决]零基础学习pythone第5讲动动手

[复制链接]
发表于 2022-2-23 21:36:00 | 显示全部楼层 |阅读模式

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

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

x
0.让玩家输入1-10的整数,输入数据类型错误时让玩家重新输入
不知道哪里出错了,希望大佬纠正教学一下(新手,尽量使用通俗语言进行讲解谢谢)
  1. import random

  2. answers = random.randint(1,10)
  3. times = 1
  4. guess = 0

  5. while (guess != answers) and (times < 3):
  6.     temp = input("从1-10选一个数:")

  7.     if temp.isdight():
  8.         guess = int (temp)
  9.         if guess > answers:
  10.             print("大了一丢丢呀,再来一次!")
  11.         else:
  12.             print("小了一点点呢,再试试!")

  13.             temp = input("再猜一下:")

  14.             if (times <= 3) and (guess == answers):
  15.                 print("聪明,这都给你猜中啦")

  16.             else:
  17.                 print("三次机会到啦,下次再玩吧")
  18.     else:
  19.         print("不听话,要输入数字:")

  20.     times = times + 1
  21.         
  22. print("游戏结束!请给个评价吧!")
复制代码


1.while temp.isdigit():以及temp.isdigit(): 是什么意思,是当变量temp是数字时进入循环吗
不知道哪里出错了,我看和答案一样呀
  1. temp = input("请输入一个闰年:")

  2. while not temp.isdigit():
  3.     temp = input("抱歉,您的输入有误,请重新输入:")

  4. year = int(temp)

  5. if year/400 == int(year/400):
  6.     print(year + "是闰年!")
  7. else:
  8.     if (year/4 == int(year/4)) and ((year/100) != int(year/100)):
  9.         print(year + "是闰年!")
  10.     else:
  11.         print(year + "不是闰年!")
  12.    
复制代码
最佳答案
2022-2-23 22:36:26
65655797 发表于 2022-2-23 22:16
修改后可以运行了,但是循环不对,是缩进还是代码顺序出错了呢,三次游戏机会有时候两次有时候四次有时候 ...

代码我改进了一下,你可以看看
  1. answers = random.randint(1, 10)
  2. times = 1
  3. guess = 0

  4. while guess != answers:
  5.     temp = input("从1-10选一个数:")

  6.     if temp.isdigit():           # isdigit 你把最后一个 i 打成了 h
  7.         guess = int(temp)

  8.         if (times <= 3) and (guess == answers):
  9.             print("聪明,这都给你猜中啦")
  10.         elif guess > answers:
  11.             print("大了一丢丢呀,再来一次!")
  12.         else:
  13.             print("小了一点点呢,再试试!")
  14.         times = times + 1  # 改进 1
  15.     else:
  16.         print("不听话,要输入数字:")

  17.     if times > 3:  # 这里有跳出,while可以不写条件
  18.         print("三次机会到啦,下次再玩吧")
  19.         break

  20. print("游戏结束!请给个评价吧!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-23 21:40:23 | 显示全部楼层
本帖最后由 isdkz 于 2022-2-23 21:52 编辑

digit 是数字的意思,字符串的 isdigit() 方法是用来判断字符串是不是整数的
  1. import random

  2. answers = random.randint(1,10)
  3. times = 1
  4. guess = 0

  5. while (guess != answers) and (times < 3):
  6.     temp = input("从1-10选一个数:")

  7.     if temp.isdigit():           # isdigit 你把最后一个 i 打成了 h
  8.         guess = int (temp)
  9.         if guess > answers:
  10.             print("大了一丢丢呀,再来一次!")
  11.         else:
  12.             print("小了一点点呢,再试试!")

  13.             temp = input("再猜一下:")

  14.             if (times <= 3) and (guess == answers):
  15.                 print("聪明,这都给你猜中啦")

  16.             else:
  17.                 print("三次机会到啦,下次再玩吧")
  18.     else:
  19.         print("不听话,要输入数字:")

  20.     times = times + 1
  21.         
  22. print("游戏结束!请给个评价吧!")

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

使用道具 举报

 楼主| 发表于 2022-2-23 22:16:46 | 显示全部楼层
isdkz 发表于 2022-2-23 21:40
digit 是数字的意思,字符串的 isdigit() 方法是用来判断字符串是不是整数的

修改后可以运行了,但是循环不对,是缩进还是代码顺序出错了呢,三次游戏机会有时候两次有时候四次有时候三次
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-23 22:21:54 | 显示全部楼层
  1. import random

  2. answers = random . randint(1,10)                 # answers 被初始化成 1 - 10 之间的一个随机数值
  3. times = 0                                        # times 被初始化为 0 (注意,是初始化为 0,不是 1)
  4. guess = 0                                        # guess 被初始化为 0,其意图是可以顺利进入 while 循环,因为,answer 的值不可能为 0

  5. while (guess != answers) and (times < 3):        # 如果 guess 不等于 answers 同时,times < 3 则开始或继续循环
  6.     temp = input("从1-10选一个数:")             # 键入一个字符串存入 temp
  7.     if temp . isdight():                         # 如果 temp 的内容是全数字
  8.         guess = int(temp)                            # 从字符串 temp 中提取数值保存到 guess
  9.         times += 1                                   # 循环次数加 1
  10.         if guess == answers:                         # 如果 guess 等于 answers
  11.             print("聪明,这都给你猜中啦")                # 给出答案正确信息
  12.         else:                                        # 否则
  13.             if guess > answers:                          # 如果 guess 比 answer 大
  14.                 print("大了一丢丢呀,再来一次!")            # 给出猜大了的提示
  15.             else:                                        # 否则
  16.                 print("小了一点点呢,再试试!")              # 给出猜小了的提示
  17.             if times < 3:                                # 如果猜测次数不到 3 次
  18.                 print("再猜一下:")                          # 提示再猜一次
  19.             else:                                        # 否则
  20.                 print("三次机会到啦,下次再玩吧")            # 提示 3 次机会已经用尽         
  21.     else:                                        # 否则(temp 内容不是纯数字)
  22.         print("不听话,要输入数字:")                # 给出键入数字提示   
  23. print("游戏结束!请给个评价吧!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-23 22:36:26 | 显示全部楼层    本楼为最佳答案   
65655797 发表于 2022-2-23 22:16
修改后可以运行了,但是循环不对,是缩进还是代码顺序出错了呢,三次游戏机会有时候两次有时候四次有时候 ...

代码我改进了一下,你可以看看
  1. answers = random.randint(1, 10)
  2. times = 1
  3. guess = 0

  4. while guess != answers:
  5.     temp = input("从1-10选一个数:")

  6.     if temp.isdigit():           # isdigit 你把最后一个 i 打成了 h
  7.         guess = int(temp)

  8.         if (times <= 3) and (guess == answers):
  9.             print("聪明,这都给你猜中啦")
  10.         elif guess > answers:
  11.             print("大了一丢丢呀,再来一次!")
  12.         else:
  13.             print("小了一点点呢,再试试!")
  14.         times = times + 1  # 改进 1
  15.     else:
  16.         print("不听话,要输入数字:")

  17.     if times > 3:  # 这里有跳出,while可以不写条件
  18.         print("三次机会到啦,下次再玩吧")
  19.         break

  20. print("游戏结束!请给个评价吧!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-23 23:03:24 | 显示全部楼层
本帖最后由 isdkz 于 2022-2-23 23:11 编辑
65655797 发表于 2022-2-23 22:16
修改后可以运行了,但是循环不对,是缩进还是代码顺序出错了呢,三次游戏机会有时候两次有时候四次有时候 ...



第一个问题:
if (times <= 3) and (guess == answers):
    print("聪明,这都给你猜中啦")
else:
    print("三次机会到啦,下次再玩吧")

这里只要为 False 都会打印“三次机会到了”,所以只要执行到了这一句你没猜中都会打印,实际上循环并没有退出。

第二个问题:
你在循环里面有两个地方有 input,但times = times + 1 只有一句,实际上的机会就不只有三次了。

第三个问题:
if guess > answers:
    print("大了一丢丢呀,再来一次!")
else:
    print("小了一点点呢,再试试!")

这里的 guess == answers 也会满足 else,所以即使你猜中了也会打印小了


第四个问题:缩进问题,你把下面这些放在了 print("小了一点点呢,再试试!") 后面,也就是说每次都打印了小了一点点,然后再让你重新输入一个数来判断有没有猜中
            temp = input("再猜一下:")

            if (times <= 3) and (guess == answers):
                print("聪明,这都给你猜中啦")

            else:
                print("三次机会到啦,下次再玩吧")


第5个问题:你应该理清楚先后顺序,先判断输入的是不是一个数字,这个没有问题,然后应该是先判断有没有猜中,没有猜中的话再是大了还是小了
       
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-24 09:29:18 | 显示全部楼层
谢谢各位大佬解答,我晚上下班回去后仔细琢磨下
各位大佬还有第二问麻烦也帮我看一下,就是第二个代码,闰年那个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-24 10:04:29 | 显示全部楼层
本帖最后由 甲鱼python 于 2022-2-24 10:07 编辑

print(year + "是闰年!")   year是数字   "是闰年"是字符串  两者不能相加  把加号改为逗号 即可
while not temp.isdigit():      # 判断输入的字符串是否只包含数字,不是就提示重新输入
    temp = input("抱歉,您的输入有误,请重新输入:")
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-24 21:31:50 | 显示全部楼层
谢谢各位大佬的解疑答惑,学到了!
最佳答案只能给一个,所以我给了我最能理解的,其他大佬不好意思啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-30 07:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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