Re: [Tutor] please help - stuck for hours
On Thu, Nov 17, 2011 at 3:19 PM, ADRIAN KELLY kellyadr...@hotmail.comwrote: 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_inbase: totalreturn=cash_in*dollar else: totalreturn=0 return totalreturn def main(): amount= while amount50: 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
Re: [Tutor] please help - stuck for hours
Where do you intend the variable cash_in to come from? The system doesn't know what cash_in is beyond that you mentioned it and that makes it impossible to multiply it with dollar which is a float type. If cash_in is supposed to be an input from the user, you probably should make it an int type right away before doing the multiplication. Hope this helps. Cheers. Sent from my BlackBerry wireless device from MTN -Original Message- From: ADRIAN KELLY kellyadr...@hotmail.com Sender: tutor-bounces+delegbede=dudupay@python.org Date: Thu, 17 Nov 2011 21:19:45 To: tutor@python.org Subject: [Tutor] please help - stuck for hours ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] please help - stuck for hours
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=1dollar=1.35 base=50if cash_inbase:totalreturn=cash_in*dollarelse: totalreturn=0return totalreturn def main():amount=0while amount50: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: waynejwer...@gmail.com Date: Thu, 17 Nov 2011 15:35:29 -0600 Subject: Re: [Tutor] please help - stuck for hours To: kellyadr...@hotmail.com CC: tutor@python.org 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=1dollar=1.35 base=50if cash_inbase:totalreturn=cash_in*dollar else:totalreturn=0return totalreturn def main(): amount=while amount50: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 modulemain() 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 exchangetotalreturn=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
Re: [Tutor] please help - stuck for hours
On Thu, Nov 17, 2011 at 4:32 PM, ADRIAN KELLY kellyadr...@hotmail.comwrote: 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?? Computers are unfailingly stupid machines. They will do whatever wrong thing you tell them to do every single time. snip def main(): amount=0 while amount50: 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 Sometimes it helps writing out the logic in steps before you translate it to code. In this case my guess is that these are the steps you want: 1. Get a value from the user (you're still using input - stop that, it's dangerous! input is only a good function in 3.x where it replaces raw_input) 2. If the value is less than 50, tell the user to enter an amount 50 and repeat step 1 3. Otherwise, exchange the amount and display that. Right now, these are the steps that you're doing: 1. Get a value from the user 2. Display the error message 3. If the value is 50, go to 1 4. exchange the amount 5. display the amount. HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] please help - stuck for hours
i know i should use input but when i changed to raw_input it wouldn't recognise the word print on the next line. honestly i have tried everything to get this working..i am 6 hrs at one program Adrian Kelly 1 Bramble Close Baylough Athlone County Westmeath 0879495663 From: waynejwer...@gmail.com Date: Thu, 17 Nov 2011 16:53:59 -0600 Subject: Re: [Tutor] please help - stuck for hours To: kellyadr...@hotmail.com CC: tutor@python.org On Thu, Nov 17, 2011 at 4:32 PM, ADRIAN KELLY kellyadr...@hotmail.com wrote: 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?? Computers are unfailingly stupid machines. They will do whatever wrong thing you tell them to do every single time. snipdef main():amount=0while amount50: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 Sometimes it helps writing out the logic in steps before you translate it to code. In this case my guess is that these are the steps you want: 1. Get a value from the user (you're still using input - stop that, it's dangerous! input is only a good function in 3.x where it replaces raw_input) 2. If the value is less than 50, tell the user to enter an amount 50 and repeat step 1 3. Otherwise, exchange the amount and display that. Right now, these are the steps that you're doing: 1. Get a value from the user 2. Display the error message 3. If the value is 50, go to 1 4. exchange the amount 5. display the amount. HTH,Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] please help - stuck for hours
On 17/11/11 23:12, ADRIAN KELLY wrote: i know i should use input but when i changed to raw_input In Python v3 use input() In python v2 input() is dangerous, use raw_input() instead. ... it wouldn't recognise the word print on the next line. Show us the exact code and error. You may be missing a parenthesis, or maybe you are just expecting the wrong behaviour... It won't execute the print until after it has read your input. everything to get this working.. i am 6 hrs at one program Heh, I've got one I've been working on for over 10 years, 6 hours is nothing! :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor