[Tutor] FW: urgent help!! THANKS EVERYONE!

Wayne Werner waynejwerner at gmail.com
Fri Nov 18 17:00:11 CET 2011


On Fri, Nov 18, 2011 at 9:26 AM, Prasad, Ramit <ramit.prasad at jpmorgan.com>wrote:

> Forwarding to the list since I wasn't the only person who helped ;)
>
> From: ADRIAN KELLY [mailto:kellyadrian at hotmail.com]
> Sent: Thursday, November 17, 2011 6:08 PM
> To: Prasad, Ramit
> Subject: RE: [Tutor] urgent help!! THANKS EVERYONE!
>
> Thanks for your help i just got it going my way below - but your way looks
> easier and better!
> thanks for all your help everyone.  feel free to comment on my method -
> its awkward but it works.......... <snip>
> def main():
>     amount=0
>     amount = float(raw_input('how much do you want to change:'))
>     while amount<50:
>         print 'enter an amount over 50'
>         amount = float(raw_input('how much do you want to change:'))
>     else:
>         total=exchange(amount)
>         print 'Your exchange comes to: ',total
>

In these sorts of cases I actually prefer the recursive solution. And from
a usability standpoint it's much nicer to warn the user ahead of time:

def amount_over_50():
    amount = float(raw_input("How much do you want to change (minimum $50)?
Amount: $"))
    if amount < 50.0:
        print 'Please enter an amount greater than $50'
        return amount_over_50()
    return amount


Then you can just do this:

def main():
    print 'Your total comes to', exchange(amount_over_50())

The chances of hitting the maximum recursion depth is pretty slim, unless
your user manages to type in a wrong number about 1000 times ;)

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111118/69ef5ee0/attachment.html>


More information about the Tutor mailing list