零基础学习pythone第5讲动动手
0.让玩家输入1-10的整数,输入数据类型错误时让玩家重新输入不知道哪里出错了,希望大佬纠正教学一下(新手,尽量使用通俗语言进行讲解谢谢)
import random
answers = random.randint(1,10)
times = 1
guess = 0
while (guess != answers) and (times < 3):
temp = input("从1-10选一个数:")
if temp.isdight():
guess = int (temp)
if guess > answers:
print("大了一丢丢呀,再来一次!")
else:
print("小了一点点呢,再试试!")
temp = input("再猜一下:")
if (times <= 3) and (guess == answers):
print("聪明,这都给你猜中啦")
else:
print("三次机会到啦,下次再玩吧")
else:
print("不听话,要输入数字:")
times = times + 1
print("游戏结束!请给个评价吧!")
1.while temp.isdigit():以及temp.isdigit(): 是什么意思,是当变量temp是数字时进入循环吗
不知道哪里出错了,我看和答案一样呀
temp = input("请输入一个闰年:")
while not temp.isdigit():
temp = input("抱歉,您的输入有误,请重新输入:")
year = int(temp)
if year/400 == int(year/400):
print(year + "是闰年!")
else:
if (year/4 == int(year/4)) and ((year/100) != int(year/100)):
print(year + "是闰年!")
else:
print(year + "不是闰年!")
本帖最后由 isdkz 于 2022-2-23 21:52 编辑
digit 是数字的意思,字符串的 isdigit() 方法是用来判断字符串是不是整数的
import random
answers = random.randint(1,10)
times = 1
guess = 0
while (guess != answers) and (times < 3):
temp = input("从1-10选一个数:")
if temp.isdigit(): # isdigit 你把最后一个 i 打成了 h
guess = int (temp)
if guess > answers:
print("大了一丢丢呀,再来一次!")
else:
print("小了一点点呢,再试试!")
temp = input("再猜一下:")
if (times <= 3) and (guess == answers):
print("聪明,这都给你猜中啦")
else:
print("三次机会到啦,下次再玩吧")
else:
print("不听话,要输入数字:")
times = times + 1
print("游戏结束!请给个评价吧!")
isdkz 发表于 2022-2-23 21:40
digit 是数字的意思,字符串的 isdigit() 方法是用来判断字符串是不是整数的
修改后可以运行了,但是循环不对,是缩进还是代码顺序出错了呢,三次游戏机会有时候两次有时候四次有时候三次 import random
answers = random . randint(1,10) # answers 被初始化成 1 - 10 之间的一个随机数值
times = 0 # times 被初始化为 0 (注意,是初始化为 0,不是 1)
guess = 0 # guess 被初始化为 0,其意图是可以顺利进入 while 循环,因为,answer 的值不可能为 0
while (guess != answers) and (times < 3): # 如果 guess 不等于 answers 同时,times < 3 则开始或继续循环
temp = input("从1-10选一个数:") # 键入一个字符串存入 temp
if temp . isdight(): # 如果 temp 的内容是全数字
guess = int(temp) # 从字符串 temp 中提取数值保存到 guess
times += 1 # 循环次数加 1
if guess == answers: # 如果 guess 等于 answers
print("聪明,这都给你猜中啦") # 给出答案正确信息
else: # 否则
if guess > answers: # 如果 guess 比 answer 大
print("大了一丢丢呀,再来一次!") # 给出猜大了的提示
else: # 否则
print("小了一点点呢,再试试!") # 给出猜小了的提示
if times < 3: # 如果猜测次数不到 3 次
print("再猜一下:") # 提示再猜一次
else: # 否则
print("三次机会到啦,下次再玩吧") # 提示 3 次机会已经用尽
else: # 否则(temp 内容不是纯数字)
print("不听话,要输入数字:") # 给出键入数字提示
print("游戏结束!请给个评价吧!") 65655797 发表于 2022-2-23 22:16
修改后可以运行了,但是循环不对,是缩进还是代码顺序出错了呢,三次游戏机会有时候两次有时候四次有时候 ...
代码我改进了一下,你可以看看
answers = random.randint(1, 10)
times = 1
guess = 0
while guess != answers:
temp = input("从1-10选一个数:")
if temp.isdigit(): # isdigit 你把最后一个 i 打成了 h
guess = int(temp)
if (times <= 3) and (guess == answers):
print("聪明,这都给你猜中啦")
elif guess > answers:
print("大了一丢丢呀,再来一次!")
else:
print("小了一点点呢,再试试!")
times = times + 1# 改进 1
else:
print("不听话,要输入数字:")
if times > 3:# 这里有跳出,while可以不写条件
print("三次机会到啦,下次再玩吧")
break
print("游戏结束!请给个评价吧!") 本帖最后由 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个问题:你应该理清楚先后顺序,先判断输入的是不是一个数字,这个没有问题,然后应该是先判断有没有猜中,没有猜中的话再是大了还是小了
谢谢各位大佬解答,我晚上下班回去后仔细琢磨下
各位大佬还有第二问麻烦也帮我看一下,就是第二个代码,闰年那个{:10_266:} 本帖最后由 甲鱼python 于 2022-2-24 10:07 编辑
print(year + "是闰年!") year是数字 "是闰年"是字符串两者不能相加把加号改为逗号 即可
while not temp.isdigit(): # 判断输入的字符串是否只包含数字,不是就提示重新输入
temp = input("抱歉,您的输入有误,请重新输入:")
谢谢各位大佬的解疑答惑,学到了!
最佳答案只能给一个,所以我给了我最能理解的,其他大佬不好意思啦
页:
[1]