[英]Python - TypeError: object of type 'NoneType' has no len()
[英]Python error: “TypeError: Object of type 'NoneType' has no len()”
我該如何解決這個錯誤? 我試圖消除用戶選擇的名稱數量。
names = []
def eliminate():
votes = int(input("How many people would you like voted off? "))
popped = random.shuffle(names)
for i in range(votes):
names.pop(len(popped))
print("The remaining players are" + names)
for i in range(0,6):
name = input("Give me a name: ")
names.append(name)
eliminate()
random.shuffle()
返回None
,而不是實際被洗牌的洗牌列表。 由於您想彈出列表中的最后一項,因此不需要為pop()
提供索引:
random.shuffle(names)
for i in range(votes):
names.pop()
random.shuffle(names)
不返回任何內容或返回None
。 但是,您的列表“名稱”正在被洗牌。 你可以得到結果:
random.shuffle(names)
for i in range(votes):
del names[-1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.