求助
有大佬知道这个程序为啥一直跳不出第一个循环不。 code? 歌者文明清理员 发表于 2023-7-7 20:15code?
counts = 3
import random
anwser = random.randint(1,10)
print("小甲鱼想的是什么数字:",end=" ")
temp = input()
# 这种想法是因为 type(1) 会返回 <class 'int'>,如果 type(temp) 返回结果一致说明输入是整数。
while type(temp) != type(1):
print("抱歉,输入不合法,", end='')
temp = input("请输入一个整数:")
while counts > 0:
temp = input()
guess = int(temp)
if guess == anwser:
print("你是小甲鱼肚子里的蛔虫吗")
print("答对了也没奖励")
break
else:
if guess < anwser:
print("你猜小了")
else:
print("你猜大了")
if counts > 0:
print("再试一次:",end="")
else:
print("机会用完了")
counts = counts -1
print("游戏结束")
互联网小白. 发表于 2023-7-7 20:18
09行改为
temp = int(input("xxx: ")) 本帖最后由 sfqxx 于 2023-7-7 20:28 编辑
这个程序一直跳不出第一个循环的原因是输入的temp始终无法转换为整数类型,导致循环条件一直成立。可以尝试以下方法解决该问题:
1. 确保输入的是数字类型。你可以在输入前对用户输入进行验证,确保输入的是合法的整数。
while True:
temp = input("请输入一个整数:")
if temp.isdigit():
break
else:
print("抱歉,输入不合法,请重新输入。")
2. 另外,还需要注意循环的顺序。在第一个循环之后,应该将输入的temp转换为整数类型,而不是在循环的开始处使用它。
请根据上述建议修改程序,应该能够解决跳不出第一个循环的问题。
3.在第9行加上int:counts = 3
import random
anwser = random.randint(1,10)
print("小甲鱼想的是什么数字:",end=" ")
temp = input()
# 这种想法是因为 type(1) 会返回 <class 'int'>,如果 type(temp) 返回结果一致说明输入是整数。
while type(temp) != type(1):
print("抱歉,输入不合法,", end='')
temp = int(input("请输入一个整数:"))
while counts > 0:
temp = input()
guess = int(temp)
if guess == anwser:
print("你是小甲鱼肚子里的蛔虫吗")
print("答对了也没奖励")
break
else:
if guess < anwser:
print("你猜小了")
else:
print("你猜大了")
if counts > 0:
print("再试一次:",end="")
else:
print("机会用完了")
counts = counts -1
print("游戏结束")
求最佳答案{:10_254:}
页:
[1]