简体   繁体   中英

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. Since you want to pop the last item in the list you do not need to provide an index to pop() :

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

random.shuffle(names) doesn't return anything or return None . However, your list "names" is being shuffled. You can get the result:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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