[Tutor] Changing the value in a list

Ken G. beachkid at insightbb.com
Thu Mar 11 17:03:58 CET 2010


Okay, now, it is understood.  Thanks.

Ken

Andre Engels wrote:
> On Thu, Mar 11, 2010 at 4:32 PM, Ken G. <beachkid at insightbb.com> wrote:
>   
>> If the following program change list[2] to 2010, replacing 1997 (and it
>> does), why doesn't the second program work in changing line[ 9:11] to 20
>> from 08?
>> FIRST PROGRAM:
>>
>>   list = ['physics', 'chemistry', 1997, 2000]
>>   print list[2]
>>   list[2] = 2010
>>   print list[2]
>>
>> OUTPUT:
>>
>>   1997
>>   2010
>>
>> SECOND PROGRAM:
>>   line = [2010020820841134]
>>   if line[ 9:11] == "08":
>>       line[ 9:11] = 20
>>       print line[ 9:11]
>>
>> OUTPUT:
>>
>>   "TypeError: 'str' object does not support item assignment."
>>
>>
>> Thanking you all in advance,
>>     
>
> First, you seem to have made a mistake in copying your programs: In
> the second program to get your error message,
>
> line = [2010020820841134]
>
> should read
>
> line = "2010020820841134"
>
> As for your question: The error message already says it better than I
> do: If x is a string, then x[9] = <something> is not allowed, nor is
> x[9:11] = <something>.
>
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100311/b39f7487/attachment.html>


More information about the Tutor mailing list