[Tutor] please help - stuck for hours
ADRIAN KELLY
kellyadrian at hotmail.com
Thu Nov 17 23:32:53 CET 2011
thanks very much, great response really really appreciated it and now i understand. i hate to ask again but can you see why it won't print the 'enter and amount over 50' in the right place??
# -*- coding: cp1252 -*-def exchange(cash_in): euro=1 dollar=1.35 base=50 if cash_in>base: totalreturn=cash_in*dollar else: totalreturn=0 return totalreturn
def main(): amount=0 while amount<50: amount = input('how much do you want to change:') print 'enter an amount over €50: ' else: total=exchange(amount) print 'Your exchange comes to: ',total main()
Adrian Kelly
1 Bramble Close
Baylough
Athlone
County Westmeath
0879495663
From: waynejwerner at gmail.com
Date: Thu, 17 Nov 2011 15:35:29 -0600
Subject: Re: [Tutor] please help - stuck for hours
To: kellyadrian at hotmail.com
CC: tutor at python.org
On Thu, Nov 17, 2011 at 3:19 PM, ADRIAN KELLY <kellyadrian at hotmail.com> wrote:
i have tried everything, i am trying to build in a loop to my 2 functions which worked fine up until my latest sorti.
please have a look if you can..............
def exchange(cash_in): euro=1 dollar=1.35
base=50 if cash_in>base: totalreturn=cash_in*dollar
else: totalreturn=0 return totalreturn
def main():
amount="" while amount<50: print 'Sorry, cannot convert an amount under €50 '
amount = input('how much do you want to change:') else:
total=exchange(amount) print 'Your exchange comes to: ',total
main()
Traceback (most recent call last):
File "F:\VTOS ATHLONE\PYTHON_VTOS\foreign exchange\f_ex3.py", line 27, in <module> main() File "F:\VTOS ATHLONE\PYTHON_VTOS\foreign exchange\f_ex3.py", line 19, in main
total=exchange(amount) File "F:\VTOS ATHLONE\PYTHON_VTOS\foreign exchange\f_ex3.py", line 7, in exchange totalreturn=cash_in*dollar
TypeError: can't multiply sequence by non-int of type 'float'
Thank you for posting the full traceback - this last line tells you the exact problem - you're trying to multiply a sequence by a float (in this case your sequence is a string). The line right above that tells you the what you tried to do: multiply `cash_in` by `dollar`.
Take a look at your code and what do you see? Well, you set `dollar = 1.35`, and you don't change the value elsewhere so that's certainly a float. What about cash_in? Well if you work your way backwards through the traceback you see that it was called with the parameter "amount".
Where is amount set? Ah, on that line:
amount = input()
This line is extremely problematic. First, I see by your print statement that you're using Python 2.x, so input is actually executing arbitrary code. What you *want* is raw_input which returns a string and is much MUCH safer.
I don't know what your input value was, but I suspect that you did something like '30' (with the quotes), because otherwise it would evaluate to an integer. It's also possible that you used `amount`, which would evaluate to "" since you already declared it. None of these things are good.
Change your initial assignment to :
amount = 0
and the input line to:
amount = float(raw_input("How much would you like to exchange? "))
This will first get a string from the user and then convert it to a float - which I suspect you'll want, since you're dealing with monetary values.HTH,Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111117/14269373/attachment.html>
More information about the Tutor
mailing list