简体   繁体   中英

How do I solve 'str object does not support item assignment'?

I am trying to change some pseudocode into Python and I came across an error.

The pseudocode is:

FOR Count - 1 to 13 DO
 OUTPUT "Please enter next digit of ISBN:"
 INPUT ISBN[Count]

My Python code is:

for Count in range (1,13):
  ISBN[Count] = int(input('Next ISBN digit:'))

I then get this errror:

TypeError: 'str' object does not support item assignment

You need to declare the array first and use .append to add new values to it.

The following example shows how.

ISBN=[];
for Count in range (1,3):
  ISBN.append( int(input('Next ISBN digit:')) )

for i in range(0,len(ISBN)):
        print ISBN[i]

If you run it with inputs 11 12 you get them printed back on screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM