0

I am very new to programming and I am just starting out with python. I found some exercises to practice a little bit and i got stuck at while and for loops.

I want to design a program that asks for a donation, and keeps asking for this donation until the minimum amount of 50 euro is donated. WHen this minimum or more is reached i want to stop the program and thank people for the donation.

My code looks like this:

donation = raw_input("enter your donation: ")

while donation < 50:
        donation= raw_input("We are sorry that's not enough, enter again: ")
        if donation >= 50 print "thank you for the donation"

but this doesn't work at all, i feel like i am missing something completely here.

Who could help me write a working code?

3 Answers 3

4

The actual problem with your code has nothing to do with the loop. As David pointed out, you can write that better, but what you have works, it's just a bit verbose.

The problem is that you're comparing strings to numbers. raw_input always returns a string. And no string is ever less than any number. So, donation < 50 will never be true.

What you need is to turn it into an int (or float or Decimal or some other kind of number, whatever's appropriate):

donation = int(raw_input("enter your donation: "))

while donation < 50:
    donation = int(raw_input("We are sorry that's not enough, enter again: "))
    if donation >= 50: print "thank you for the donation"
Sign up to request clarification or add additional context in comments.

Comments

3

The if condition within the while loop shouldn't be necessary at all. The loop will continue until donation >= 50 so you should just be able to print the message after the loop:

donation = raw_input("enter your donation: ")

while donation < 50:
        donation= raw_input("We are sorry that's not enough, enter again: ")

print "thank you for the donation"

6 Comments

To expand upon this: for loops are used when you know how many times you're going to iterate through the loop. While loops are to be run until some condition is true (which may happen now or in 50 years..)
@statue: Indeed. while loops make for great potentially-infinite loops, such as a "game loop" where the cycle of processing continues until some UI interaction terminates the game, for example.
This is a good suggestion, but it doesn't actually make his code work, or explain why it doesn't work or what he's missing. It's more of a comment than an answer. (But a very good comment, if so.)
@abarnert: I disagree, given that the problem description was "it doesn't work at all" and the question was "Who could help me write a working code?" Both answers address the need. Mine focuses on the structure, yours on the data types.
@David: But his code is already working without your fix, and in fact works exactly the same; it's just more verbose and less readable.
|
-1
if donation >= 50: print "thank you for the donation"

Comments

Your Answer

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.