我正在尝试使此脚本从“复仇者”和“平民”列表中选择一个随机值。我这样做了,我选择了一个随机值作为获胜者。我正在尝试做的是使获胜者留在列表中,而输者则从列表中删除。 (例如:如果“ selectedAvenger”获胜,“ selectedVillian”将从“平民”列表中删除,反之亦然。)(编程类的小型项目)
import random # imports the random module
avengers = ['Thor', 'Iron Man', 'Spiderman', 'Hulk', 'Falcon', 'Black Widow', 'Antman', 'Captain America']
verb = ['smashed', 'kicked', 'slapped', 'bonked']
villian = []
# these are the lists that the script will use
for i in range(8):
villian.append(input('Enter villian number ' + str(i + 1))) # asks user to list 8 villians
print('Welcome to Avengers: End of Line!') # welcomes the user
input('Press "Enter" to continue.') # asks the user to press the enter key to continue the script
selectedAvenger = random.choice(avengers) # selects a random avenger
selectedVillian = random.choice(villian) # selects a random villian
selectedVerb = random.choice(verb) # selects a random verb
winner = random.choice([selectedAvenger, selectedVillian])
print(winner)
答案 0 :(得分:0)
我假设您有一个恶棍填充列表?我想我可能无法正确理解您的问题,但是据我所知,这是解决方案。
if winner == selectedAvenger:
villian.remove(selectedVillian)
else:
avengers.removed(selectedAvenger)
答案 1 :(得分:0)
import random # imports the random module
avengers = ['Thor', 'Iron Man', 'Spiderman', 'Hulk', 'Falcon', 'Black Widow', 'Antman', 'Captain America']
verb = ['smashed', 'kicked', 'slapped', 'bonked']
villian = []
# these are the lists that the script will use
for i in range(8):
villian.append(input('Enter villian number ' + str(i + 1))) # asks user to list 8 villians
print('Welcome to Avengers: End of Line!') # welcomes the user
input('Press "Enter" to continue.') # asks the user to press the enter key to continue the script
selectedAvenger = random.choice(avengers) # selects a random avenger
selectedVillian = random.choice(villian) # selects a random villian
selectedVerb = random.choice(verb) # selects a random verb
print([selectedAvenger, selectedVillian])
listPossibleChoice = [selectedAvenger, selectedVillian]
winner = random.choice([selectedAvenger, selectedVillian])
print(winner)
heroOrVillain = listPossibleChoice.index(winner) #if the index is 0, we delete from avengers, if it is 1, we delete from villains.
print(heroOrVillain)
if (heroOrVillain==0):
avengers.remove(selectedAvenger)
print(avengers)
else:
villian.remove(selectedVillian)
print(villian)
我相信这应该可以打印出您想要的结果。这是我的控制台中的输出: