Python,无需重复即可提出问题

时间:2016-12-07 15:34:34

标签: python if-statement

#TODO: QUESTION 1
    num = random.randint(1, 100)
    print(num)
    # Male or Female probabilities = 35%
    if num > 0 and num <= 18:
        femaleCheck(People)
        printPeopleList(People)
    if num > 18 and num <= 35:
        maleCheck(People)
        printPeopleList(People)
    # Wearing a T-Shirt probabilities = 15%
    if num > 35 and num <= 50:
        tShirtCheck(People)
        printPeopleList(People)
    #Eye Color probabilities 25%
    if num > 50 and num <= 63:
        eyeColorBrownCheck(People)
        printPeopleList(People)
    if num > 63 and num <= 75:
        eyeColorBlueCheck(People)
        printPeopleList(People)
    #Showing Teeth probabilities 25%
    if num > 75 and num <= 100:
        showingTeethCheck(People)
        printPeopleList(People)

以下是猜测我创建的游戏代码的一小部分。当前它实现时,它从列表中选择一个随机数,并询问相应的数字以执行一个功能。一旦问到问题1,我希望它在剩余的可用选项中提出问题2。问题3将包括剩余的2个选项,并且还会引入一个新功能。我正在努力弄清楚哪种方法最有效;我知道我可以随后缩进每个问题,但那将是非常混乱和难以阅读。是否有一些方法可用于以简单的方式提出以下问题?可能从随机整数中删除该范围?

1 个答案:

答案 0 :(得分:3)

如果您使用的是3.6,则可以使用random.choices对您的函数使用checkFunction = random.choices([femaleCheck, maleCheck, tShirtCheck, ...], weights=[35, 15, 25, ...]) checkFunction(People) printPeopleList(People) 关键字参数。

 let webPath =  URL(string: "http://peterlinnik.com/Plex%20Music/The%20Beatles%20-%20Discography/Abbey%20Road/")

        do {
            // Get the directory contents urls (including subfolders urls)
            let webFiles = try FileManager.default.contentsOfDirectory(at: webPath!, includingPropertiesForKeys: nil, options: [])
            print(webFiles)


        } catch let error as NSError {
            print(error.localizedDescription)
        }

如果没有,请查看this answe r如何实现类似的内容。