On Thu, Nov 17, 2011 at 3:19 PM, ADRIAN KELLY <kellyadr...@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
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to