Python错误:“TypeError:类型为'NoneType'的对象没有len()

2024-09-29 01:25:12 发布

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

如何修复此错误?我试图消除用户选择的名称数量

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()

Tags: 用户namein名称forinput数量names
2条回答

random.shuffle(names)不返回任何内容或返回None。但是,您的“姓名”列表正在被洗牌。 您可以得到以下结果:

random.shuffle(names)
for i in range(votes):
    del names[-1]

^{}返回None,而不是实际已被洗牌的洗牌列表。由于要弹出列表中的最后一项,因此不需要为pop()提供索引:

random.shuffle(names)
for i in range(votes):
    names.pop()

相关问题 更多 >