[Tutor] infinite loop

Alan Gauld alan.gauld at btinternet.com
Fri Nov 18 01:06:44 CET 2011

On 17/11/11 21:29, ADRIAN KELLY wrote:

> amount=float()

You don;t need this line because you assign a value to amount
immediately you run main()

> def main():
> amount = float(raw_input('how much do you want to change:'))
> while amount<50:
>    print 'Sorry, cannot convert an amount under €50 '

To get a while loop to terminate you must change something about the 
test condition.
In this case the test value 50 is a constant so it needs to be amount 
that changes. But you only print a message... You need to read a new amount.

> else:
>    total=exchange(amount)
>    print 'Your exchange comes to: ',total

You don't really want/need the else: line.
It's not wrong but its more normal to see it done like this:

while <test-condition>
      loop body here
next statement

with no explicit else.

In fact, thinking about it, I've never found a use for
the while/else construct, has anyone else?

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list