【问题标题】:How to tell that the strings selected randomly out of a list are the same如何判断从列表中随机选择的字符串是否相同
【发布时间】:2017-06-15 21:28:46
【问题描述】:

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

  1. 让程序通过输入将 7 只动物添加到列表中 (DONE)
  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()

【问题讨论】:

  • “我试过到处搜索并尝试了很多方法,但代码没有做正确的事情。” 您的代码似乎没有尝试任何方法全部。请与我们分享您尝试的最后一件事。
  • @Kevin if (generatedList==a,a,a): print("Bingo")

标签: python string list random


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    相关资源
    最近更新 更多