Home > Back-end >  While loop not working in python, can someone please chack?
While loop not working in python, can someone please chack?

Time:06-10

I'm working on a school project, and I need to create a program that keeps asking for money until an amount larger than 50 is entered. I have written this code, but it doesn't work. Can someone please help me? With kind regards, Silke

amount = int(input("Enter the amount you want to donate: "))
while amount < 50:
    amount = input("Enter the amount you want to donate: ")

print("Thank you very much for your contribution of " %amount)

CodePudding user response:

You forgot to call int() for the second input.

I generally recommend that you avoid duplicate code like that. Do the input() only once, inside the loop, instead of repeating it before and inside.

while True:
    amount = int(input("Enter the amount you want to donate: "))
    if amount >= 50:
        break

This way, if you need to change it, you only have to change one place.

CodePudding user response:

You can try this.

while True:
    amount = int(input("Enter the amount you want to donate: "))
    if amount > 50:
        break

print("Thank you very much for your contribution of", amount)
  • Related