简体   繁体   English

如何解决“str对象不支持项目分配”?

[英]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.我正在尝试将一些伪代码更改为 Python,但遇到错误。

The pseudocode is:伪代码是:

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

My Python code is:我的 Python 代码是:

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.您需要先声明数组并使用.append向其添加新值。

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.如果您使用输入 11 12 运行它,则会将它们打印回屏幕上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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