[Tutor] Is this correct syntax for what I want?
Nathan Pinno
falcon3166 at hotmail.com
Sun Jun 25 07:57:42 CEST 2006
Alan and all,
Ok, so I changed it to a dictionary, and when I tested it, this error came
up:
Traceback (most recent call last):
File "C:\Python24\Account Tracker.py", line 91, in -toplevel-
printall()
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?
Thanks for the help so far!
----- Original Message -----
From: "Alan Gauld" <alan.gauld at freenet.co.uk>
To: "Nathan Pinno" <falcon3166 at hotmail.com>; <tutor at python.org>
Sent: Saturday, June 24, 2006 3:54 PM
Subject: Re: [Tutor] Is this correct syntax for what I want?
>
>> The data structure is:
>> mydata = [(Checking, 12.50),(Savings, 34.50)] And I want the result to
>> look like this:
>> mydata = [(Checking, 19.50),(Savings, 34.50)]
>>
>> So how do I do this?
>
> OK, The problem is that you cannot change the contents of a
> tuple, you can only create a new tuple. Therefore you are probably better
> changing to a list or dictionary.
>
> From the look of the data I'd suggest a dictionary might be best:
>
> myData = { 'Checking': 12.50, 'Savings': 34.50}
>
> Then you can do:
>
> myData['Checking'] += 7.00
>
> If you decide to go for a list then it would look like:
>
> myData = [['Checking', 12.5],['Savings',34.50]]
>
> And you would modify it with:
>
> myData[0][1] += 7 # [0][1] = second element of first list
>
> But I think you will agree the dictionary approach looks a lot nicer, so
> unless there is a good reason why you can't use that I'd strongly
> recommend a dictionary.
>
> 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