[Tutor] please help - stuck for hours
waynejwerner at gmail.com
Thu Nov 17 22:35:29 CET 2011
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):
> if cash_in>base:
> return totalreturn
> def main():
> while amount<50:
> print 'Sorry, cannot convert an amount under €50 '
> amount = input('how much do you want to change:')
> print 'Your exchange comes to: ',total
> Traceback (most recent call last):
> File "F:\VTOS ATHLONE\PYTHON_VTOS\foreign exchange\f_ex3.py", line 27,
> in <module>
> File "F:\VTOS ATHLONE\PYTHON_VTOS\foreign exchange\f_ex3.py", line 19,
> in main
> File "F:\VTOS ATHLONE\PYTHON_VTOS\foreign exchange\f_ex3.py", line 7, in
> 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
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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor