gpt4 book ai didi

python - python 中的 while 循环不会停止

转载 作者:行者123 更新时间:2023-11-28 22:08:52 28 4
gpt4 key购买 nike

我对 python 有点陌生,在尝试制作 while 循环时遇到了困难。每当我运行代码时,无论项目的值(value)如何,它都会不断提示我输入。


print("what is your name")
name = input("")
print("hello " + name)
print("how many inventory items do you want to buy")
items = input()
while (items > 0):
print("what is the inventory number of the item")
inventoryNum = int(input())
if (inventoryNum >= 1000 and inventoryNum <= 1999 ):
print("the item is on the lower level")

elif (inventoryNum == 8005 or inventoryNum == 8000):
print("the item is on the mezzanine")

elif (inventoryNum > 1999 and inventoryNum <= 5000 or inventoryNum > 9000):
print("the item is on the main floor")

elif (inventoryNum > 5000 and inventoryNum <= 9000 and inventoryNum != 8005 and inventoryNum != 8000):
print("the item is on the upper level")
items = items - 1

最佳答案

这只是一个缩进问题。 缩进您的 items = items - 1 因为它在您最后的 elif 语句中。

while (items > 0):
print("what is the inventory number of the item")
inventoryNum = int(input())
if (inventoryNum >= 1000 and inventoryNum <= 1999 ):
print("the item is on the lower level")

elif (inventoryNum == 8005 or inventoryNum == 8000):
print("the item is on the mezzanine")

elif (inventoryNum > 1999 and inventoryNum <= 5000 or inventoryNum > 9000):
print("the item is on the main floor")

elif (inventoryNum > 5000 and inventoryNum <= 9000 and inventoryNum != 8005 and inventoryNum != 8000):
print("the item is on the upper level")
items = items - 1

关于python - python 中的 while 循环不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58074011/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com