'Why is my while loop printing continously in the python IDLE shell

I am trying to write a loop that keeps asking the user for the number of items they bought, only stopping when the user inputs 0 or less. Here is my code:

quantity = int(input("Enter the amount of items bought: "))
while (quantity>0):
    print("You bought", quantity, 'items')

The problem is that this "print("You bought", quantity, 'items')" keeps printing continuously in the IDLE shell. Any help is appreciated.



Solution 1:[1]

In the snippet given, it seems you haven't changed the variable quantity after buying the item. I would suggest removing the loop entirely, if there is a currency value. Try this:

quantity = int(input("Enter the amount of items bought: ")) # Ask for amount bought 
print("You bought"+str(quantity)+" items.") # Buy message
currency-=cost_of_item # Replace cost of item with the cost of what you are buying and currency with the money variable

However, you could also try this if you still want to keep the loop inside your code:

quantity = int(input("Enter the amount of items bought: "))
while (quantity>0):
    print("You bought", quantity, 'items') # Same code as before
    quantity=0 # Exit loop 

Solution 2:[2]

A loop makes code run over and over until it reaches a condition. Your code says

quantity = whatever the user inputs

while quantity is > than 0 print this statement

You're going to need to place the user input inside of your loop so it will say instead

while quantity is smaller than 0 ask a user for input

Solution 3:[3]

Change it to this:

while (quantity>0):
    quantity = int(input("Enter the amount of items bought: "))
    print("You bought", quantity, 'items')

This way, you are asking for quantity every iteration.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FatPancake52
Solution 2 sss
Solution 3 Sid Nutthi