簡體   English   中英

如何解決“str對象不支持項目分配”?

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

我正在嘗試將一些偽代碼更改為 Python,但遇到錯誤。

偽代碼是:

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

我的 Python 代碼是:

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

然后我得到這個錯誤:

TypeError: 'str' object does not support item assignment

您需要先聲明數組並使用.append向其添加新值。

下面的例子展示了如何。

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

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

如果您使用輸入 11 12 運行它,則會將它們打印回屏幕上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM