2

i'm a little new to python and I've ran into difficulty when trying to make a while loop. Whenever i run the code, it keeps prompting me for input no matter the value of items there are.


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
CodeRed
9031 gold badge7 silver badges24 bronze badges
asked Sep 24, 2019 at 6:07

4 Answers 4

1

This is just an indention issue. Outdent your items = items - 1 because it is inside your last elif statement.

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
answered Sep 24, 2019 at 6:14
Sign up to request clarification or add additional context in comments.

Comments

1

Look at the code below. There are mainly two issues with your code which are commented:

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
answered Sep 24, 2019 at 6:22

Comments

1

I see your problem and I noticed a couple of things:

  1. The first problem is a minor one but can be quickly fixed:
items = input()

This should be:

items = int(input())

Since whenever we want to obtain a value from an input function, we're going to get a string type, no matter if you pass a number through it. To remedy this we make the string into an integer by putting an int in front of it

  1. Simplification of code and indenting problem:
print("what is the inventory number of the item")
inventoryNum = int(input())

Can be simplified to:

inventoryNum = int(input("What is the inventory number of the item: "))

-Furthermore, at the end since it appears that all of the items that aren't part of the first three conditions associated with the inventoryNum are on the main floor you can just use an else statement. Like so:

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")
  1. Finally, in order for the while loop to work the decreasing of the item value by 1 has to be indented outside, otherwise the value will only decrease by one when one of the elif statements is the path it takes:
 print("the item is on the upper level")
 items = items - 1

Should be:

 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 

Hope this helps.

answered Sep 24, 2019 at 6:41

Comments

1

Another way to deal with this could be make a function to ask if you wish to continue performing the program also, for example:

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();

I have noticed that your if statements doesn't have any termination point, for example in if < 1000, so because that every time that you typed any value less than this interval you are supposed to type over and over, i did some fix in your inputs (as like some mates above already did) and you should put an else statement at the final when your inventory number isn't in any category:

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

So, each time that you will type in Yes it continues, in opposite it will close the application

answered Sep 24, 2019 at 7:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.