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

Alan Gauld alan.gauld at freenet.co.uk
Sun Jun 25 09:07:10 CEST 2006


>  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