简体   繁体   中英

Error: TypeError: 'str' object cannot be interpreted as an integer while trying to pick a random winner (Python)

I'm kind of new to programming and I've been trying to mess with Python a bit but while I was trying to make a random name picker that picks a random name from a list I always end up getting the same error .I also try to remove the winner that the code chose and add a new name to the 'winners' list but it doesn't seem to work. This is the short code I have right now.

winners = ["Wane", "Trevor", "Franklin", "Martoz"]
winner = random.choice(winners)
winners.pop(winner)
winners.append("Michael")
print(winners)

If you want to remove an item from a list, use the .remove() function like so:

>>> winners = ["a", "b", "c"]
>>> winners.remove("b")
>>> winners
['a', 'c']

.pop() is used to extract an element from a list based on the index given, and returns the element removed from the list. Your error appears because you're trying to pop based off an element in the list (a name), as opposed to its index (its position in the list).

If you want to stick with pop , perhaps instead of choosing a random name, choose a random index in the list:

winners = ["Wane", "Trevor", "Franklin", "Martoz"]
winner = winners.pop(random.randrange(len(winners)))

If you provide an argument for method list.pop , that argument must be of type integer and represent the index of the element in the list.

Since your call looks like

winners.pop(winner)

and winner is a string, you'll get the mentioned error

TypeError: 'str' object cannot be interpreted as an integer

Also, I'm not sure what you really want to do, but you probably need to have two lists participants and winners . The following example draws two winners from the list:

import random

participants = ["Wane", "Trevor", "Franklin", "Martoz"]
winners = []

print("Participants before:", participants)
print("Winners before:", winners)

winner = random.choice(participants)
winners.append(winner)
participants.remove(winner)

winner = random.choice(participants)
winners.append(winner)
participants.remove(winner)

print("Participants after:", participants)
print("Winners after:", winners)

Which prints:

Participants before: ['Wane', 'Trevor', 'Franklin', 'Martoz']
Winners before: []
Participants after: ['Wane', 'Trevor']
Winners after: ['Martoz', 'Franklin']

Instead of using .pop() , just use remove()

winners = ["Wane", "Trevor", "Franklin", "Martoz"]
winner = random.choice(winners)
winners.remove(winner)
winners.append("Michael")
print(winners)

If you want to use pop() function you should provide the index of winner in the winners list. This can be done by using winners.index(winner)

winners = ["Wane", "Trevor", "Franklin", "Martoz"]
winner = random.choice(winners)
winners.pop(winners.index(winner))
winners.append("Michael")
print(winners)

Result:

['Wane', 'Trevor', 'Martoz', 'Michael']

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