简体   繁体   English

Python 错误:“TypeError:“NoneType”类型的 Object 没有 len()”

[英]Python error: “TypeError: Object of type 'NoneType' has no len()”

How can I fix this error?我该如何解决这个错误? I am attempting to eliminate the number of names the user chooses.我试图消除用户选择的名称数量。

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() returns None , not the shuffled list which is actually shuffled in place.random.shuffle()返回None ,而不是实际被洗牌的洗牌列表。 Since you want to pop the last item in the list you do not need to provide an index to pop() :由于您想弹出列表中的最后一项,因此不需要为pop()提供索引:

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

random.shuffle(names) doesn't return anything or return None . random.shuffle(names)不返回任何内容或返回None However, your list "names" is being shuffled.但是,您的列表“名称”正在被洗牌。 You can get the result:你可以得到结果:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM