[Tutor] Is this correct syntax for what I want?

Nathan Pinno falcon3166 at hotmail.com
Sun Jun 25 09:13:23 CEST 2006

Thanks, it works now perfectly! Thanks for all the help!
----- Original Message ----- 
From: "Alan Gauld" <alan.gauld at freenet.co.uk>
To: "Nathan Pinno" <falcon3166 at hotmail.com>; <tutor at python.org>
Sent: Sunday, June 25, 2006 1:07 AM
Subject: Re: [Tutor] Is this correct syntax for what I want?

>>  File "C:\Python24\Account Tracker.py", line 49, in printall
>>    print account,"\t $",accountlist[account]+"\n"
>> TypeError: unsupported operand type(s) for +: 'float' and 'str'
>> So how do I fix this error?
> What it's saying is you can't add a float and string. If you 
> look at the code you are trying to add "\n" to accountlist[account] 
> which is a float. You need to convert the string to a float or 
> the float to a string. In this case converting the float to a 
> string would be the better approach! :-)
> However I would personally recommend that you use a 
> format string here since it will give you much better control 
> of the appearance of the output and avoids the need to 
> convert the values.
> print "%s\t $%0.2f\n" % (account, accountlist[account])
> HTH,
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld

More information about the Tutor mailing list