簡體   English   中英

“列表”對象不能解釋為整數

[英]'list' object cannot be interpreted as an integer

Player1Word = input("input word here 10 letters max")
length = (list(map(int(len, Player1Word.split()))))
print(length)

由於長度變量是列表,我無法在while循環中設置條件,這是一個錯誤。

Traceback (most recent call last):
  File "python", line 2, in <module>
TypeError: 'list' object cannot be interpreted as an integer

這是錯誤消息,我正在嘗試制作,因此如果長度等於另一個變量,則while不會被激活。

while Player2 < 15 and hangman < 12 and rightletters < length:

如果我像這樣從原始代碼中刪除int()。

Player1Word = input("input word here 10 letters max")
length = (list(map(len, Player1Word.split())))
print(length)

給出以下錯誤信息。

Traceback (most recent call last):
File "python", line 8, in <module>
TypeError: '<' not supported between instances of 'int' and 'list'

您正在做一些多余的計算。 萬一需要計算用戶輸入的單詞數,您需要什么:

length = len(Player1Word.split()) 

否則,如果僅用於獲取Player1Word總長度 ,則只需len就足夠了。

length = len(Player1Word) 

只需使用len函數即可獲取字符串的總長度。

len(s)

返回對象的長度(項目數)。 參數可以是序列(例如字符串,字節,元組,列表或范圍)或集合(例如字典,集合或凍結集合)。

因此,您所需要做的就是:

length = len(Player1Word) 

暫無
暫無

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

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