“list”对象不能解释为整数

2024-10-01 15:32:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我有一个错误,我不能在while循环中设置条件,因为length变量是一个列表。在

^{pr2}$

这是一个错误消息,我正在尝试使它,这样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'

Tags: andmapinputlenhere错误lengthmax
2条回答

只需使用len函数来获得字符串的总长度。在

len(s)

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

所以,你需要做的就是:

length = len(Player1Word) 

你在做一些多余的计算。如果您需要计算字数用户输入的内容:

length = len(Player1Word.split()) 

否则,如果只是为了得到Player1Word总长度,那么只要len就足够了。在

^{pr2}$

相关问题 更多 >

    热门问题