gpt4 book ai didi

python - 如何判断从列表中随机选择的字符串是相同的

转载 作者:行者123 更新时间:2023-11-30 22:42:58 26 4
gpt4 key购买 nike

我有这个问题;我将分步骤总结一下

  1. 让程序通过输入将 7 只动物添加到列表中(完成)
  2. 让程序从列表中随机选择 3 只动物(完成)
  3. 如果选择的动物都相同,则打印“Bingo!” ;如果没有,则循环输入,直到用户按“X”或得到“Bingo!”。

我在最后一步中遇到了麻烦,我尝试到处搜索并尝试了很多方法,但代码没有做正确的事情。代码如下:

import random

bingoList=[]
generatedList=[]

def generateCards():
for i in range(0,3):
print(random.choice(bingoList))
generatedList.append(a)

for i in range(0,7):
a=input("Enter an animal: ")
if (a.isdigit()==False):
bingoList.append(a)
else:
printprint("You are not allowed to input numbers; program stopping...")
exit()

print("All the animals have been stored, let's start the game..")

generateCards()

最佳答案

假设我正确理解你的问题,你应该使用一组:

if len(set(bingoList)) == 1:
print('Bingo!')

关于python - 如何判断从列表中随机选择的字符串是相同的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41940729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com