首页 > 解决方案 > python中的while循环没有停止

问题描述

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


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

标签: pythonwhile-loop

解决方案


这只是一个缩进问题。突出您的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

推荐阅读