Python 我不知道';我没有任何错误,我的循环一直在运行。我做错了什么?

Python 我不知道';我没有任何错误,我的循环一直在运行。我做错了什么?,python,Python,我试图做一个循环,检测用户何时吃了足够的食物。这个代码对我来说很有意义,但显然是错的。我觉得这和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 f

我试图做一个循环,检测用户何时吃了足够的食物。这个代码对我来说很有意义,但显然是错的。我觉得这和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
尝试=0
当食物<10和<3时:
食物=食物+int(输入(“你想吃多少食物?”:)
打印(“你吃了”+str(食物)+“食物量”。)
尝试次数+=1
食物=食物+int(输入(“食物不够,你还想吃多少?:”)
打印(“你吃了”+str(食物)+“食物量”。)
尝试次数+=1
食物=食物+int(输入(“还不够。你还有一次机会吃东西。还有多少?:”)
打印(“你吃了”+str(食物)+“食物量”。)
尝试次数+=1
打印(“太晚了,你死了”)
其他:
打印(“你已经吃了足够的食物。”)

您没有循环,只有一个输入序列:

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.")

当用户输入非数字字符时会出现问题。由于
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.")
food=0
尝试=0
当食物<10和<3时:
食物=食物+int(输入(“你想吃多少食物?”:)
打印(“你吃了”+str(食物)+“食物量”。)
尝试次数+=1
食物=食物+int(输入(“食物不够,你还想吃多少?:”)
打印(“你吃了”+str(食物)+“食物量”。)
尝试次数+=1
食物=食物+int(输入(“还不够。你还有一次机会吃东西。还有多少?:”)
打印(“你吃了”+str(食物)+“食物量”。)
尝试次数+=1
打印(“太晚了,你死了”)
如果(食物>=10或尝试>=3):
打印(“你已经吃了足够的食物。”)

您可以参考

您能提供更多的上下文吗?i、 e.您在什么环境中运行此代码?什么语言?等等?虽然这个代码布局不好,但对我来说,它运行正确,循环结束。如果你说的是为什么在食物超过10后它仍继续运行,那么你的代码对我来说很好。。这是因为while部分在循环的每次迭代中只计算一次,而您的代码被编写为只运行一次迭代,因为您在一次迭代中添加了3次尝试。我想您想了解一下
break
语句的作用。while循环的整个主体将至少运行一次,因此即使第一次输入超过10,它也会再次提示用户。正如前面所说,逻辑确实很糟糕:)我也同意@jszjsz。我建议结合我们所有的代码以获得最佳结果。改进:您可以使用allowedChars中的
if str(input)[I]替换第二个for循环。。。
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.")