python - 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
解决方案
这只是一个缩进问题。突出您的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
推荐阅读
- javascript - 带有 CLICK 事件侦听器的 Javascript API FETCH:TypeError:尝试获取资源时出现 NetworkError
- datepicker - jQuery Datepicker - 如何设置:datepicker1 + 1 天变为 minDate datepicker2 并可选择关闭周六和周日
- python - 尽管为给定列提供“非空”条件,但我似乎可以在表中输入空值
- android - 找不到符号导入 io.flutter.FlutterInjector
- php - 在 postgresql 中存储在数据类型为 double 的列中时,减法结果不准确
- string - 将格式错误的字符串转换为 Scala 类
- r - 如何在使用 MICE 进行多重插补后进行描述性统计(中位数、IQR、频率、比例等)
- python - 如何按位置搜索 Instagram 帖子
- java - 调整作为单例且具有最终访问修饰符的方法的返回值
- kubernetes - 在 kubernetes 主机上挂载 glusterfs