我是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
答案 0 :(得分: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
答案 1 :(得分:1)
查看下面的代码。您的代码主要存在两个问题,它们被注释了:
print("what is your name")
name = input("")
print("hello " + name)
print("how many inventory items do you want to buy")
items = int(input()) #You forgot to convert the input from str to int
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")
#you put the code in the scope of elif; it should be in the scope of while loop.
#the problem arised due to the indentation.
items = items - 1
答案 2 :(得分:1)
我看到了您的问题,并且注意到了几件事:
items = input()
这应该是:
items = int(input())
由于无论何时我们想从输入函数中获取一个值,无论您是否传递数字,我们都将获得一个字符串类型。为了解决这个问题,我们将一个int放在字符串前面使它成为一个整数
print("what is the inventory number of the item")
inventoryNum = int(input())
可以简化为:
inventoryNum = int(input("What is the inventory number of the item: "))
-此外,最后,由于似乎所有不属于与stockNum相关的前三个条件的项目都位于主楼层,因此您可以使用else语句。像这样:
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")
else:
print("The item is on the upper level")
print("the item is on the upper level")
items = items - 1
应该是:
inventoryNum = int(input("What is the inventory number of the item: "))
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")
else:
print("The item is on the upper level")
#items -= 1 is indented outside of the if/elif/else statement since you want the value of items to decrease after each iteration
items -= 1 #Simpler way of saying items = items - 1, works with + and / as well
print("All items registered. End of program") #Thought that this would be a nice way of notifying the user that they've reached the end of the program
希望这会有所帮助。
答案 3 :(得分:1)
处理此问题的另一种方法是使函数询问您是否还要继续执行该程序,例如:
def Continue():
answer = input("Do you want to continue? (Yes/No - Y/N)")
if(answer == "Yes" or answer == "Y"):
pass
elif(answer == "No" or answer == "N"):
print("Goodbye")
sys.exit();
我注意到您的if语句没有任何终止点(例如在if < 1000
中),因为每次您键入的值小于此间隔时,都应该反复键入,我对您的输入进行了一些修复(就像上面的一些伴侣所做的一样),并且当您的库存编号不在任何类别中时,您应该在最后输入else语句:
name = input("what is your name: ")
print("hello " + name)
items = int(input("how many inventory items do you want to buy: "))
while (items > 0):
inventoryNum = int(input("what is the inventory number of the item: "))
if (inventoryNum >= 1000 and inventoryNum <= 1999):
print("the item is on the lower level")
Continue()
elif (inventoryNum > 1999 and inventoryNum <= 5000 or inventoryNum > 9000):
print("the item is on the main floor")
Continue()
elif (inventoryNum == 8005 or inventoryNum == 8000):
print("the item is on the mezzanine")
Continue()
elif (inventoryNum > 5000 and inventoryNum <= 9000 and inventoryNum != 8005 and inventoryNum != 8000):
print("the item is on the upper level")
Continue()
else:
print("item is not registered")
Continue()
items = items - 1
因此,每次您输入“是”时,它都会继续,相反,它将关闭应用程序