[英]I don't have any errors and my loop just keeps running. What am I doing wrong?
我正在尝试制作一个循环来检测用户何时吃够了食物。 这段代码对我来说很有意义,但显然是错误的。 我觉得这与从 int 到 str 的转换有关,反之亦然,但我不确定。
food = 0
tries = 0
while food < 10 and tries < 3:
food = food + int(input("How much food would you like to eat?: "))
print("You've eaten " + str(food) + " amounts of food.")
tries += 1
food = food + int(input("It's not enough food. How much more would you like to eat?: "))
print("You've eaten " + str(food) + " amounts of food.")
tries += 1
food = food + int(input("It's still not enough. You have one more chance to eat. How much more?: "))
print("You've eaten " + str(food) + " amounts of food.")
tries += 1
print("It's too late. You're dead")
else:
print("You've eaten enough food.")
您没有循环,但有一个输入序列:
food = 0
food = food + int(input("How much food would you like to eat?: "))
print(f"You've eaten {food} amounts of food.")
if food < 10:
food = food + int(input("It's not enough food. How much more would you like to eat?: "))
print(f"You've eaten {food} amounts of food.")
if food < 10:
food = food + int(input("It's still not enough. You have one more chance to eat. How much more?: "))
print(f"You've eaten {food} amounts of food.")
if food < 10:
print("It's too late. You're dead")
if food >= 10:
print("You've eaten enough food.")
您可以使用 for 循环将其转换为对输入文本的循环:
food = 0
for text in [
"How much food would you like to eat?: ",
"It's not enough food. How much more would you like to eat?: ",
"It's still not enough. You have one more chance to eat. How much more?: ",
]:
food += int(input(text))
print(f"You've eaten {food} amounts of food.")
if food >= 10:
print("You've eaten enough food.")
break
else:
print("It's too late. You're dead")
当用户输入非数字字符时会出现此问题。 由于int
方法不过滤字符串,因此您需要手动执行此操作。 按照@Daniel 的代码,我在代码中添加了过滤。 这是带有实际循环的工作代码:
food = 0
def filterNonNumChars(input):
input = str(input)
allowedChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
output = ""
for i in range(0, len(input)):
if input[i] in allowedChars:
output += input[i]
return output
for text in [
"How much food would you like to eat?: ",
"It's not enough food. How much more would you like to eat?: ",
"It's still not enough. You have one more chance to eat. How much more?: ",
]:
food += int(filterNonNumChars(input(text)))
print(f"You've eaten {food} amounts of food.")
if food >= 10:
print("You've eaten enough food.")
break
else:
print("It's too late. You're dead")
不确定你的循环的逻辑。 “while ... else ...”并不常见和清晰,可以用“while”和“if”代替,逻辑清晰。
food = 0
tries = 0
while food < 10 and tries < 3:
food = food + int(input("How much food would you like to eat?: "))
print("You've eaten " + str(food) + " amounts of food.")
tries += 1
food = food + int(input("It's not enough food. How much more would you like to eat?: "))
print("You've eaten " + str(food) + " amounts of food.")
tries += 1
food = food + int(input("It's still not enough. You have one more chance to eat. How much more?: "))
print("You've eaten " + str(food) + " amounts of food.")
tries += 1
print("It's too late. You're dead")
if (food >= 10 or tries >= 3):
print("You've eaten enough food.")
你可以参考这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.