【问题标题】:make a random quiz with list in python在 python 中使用列表进行随机测验
【发布时间】:2017-12-01 00:32:38
【问题描述】:
    def ask_questions():
        choice = (random.choice(question))    
        print(choice)
        if choice == 0:
            print options[0]
            answer0 = raw_input(inputs)
        if answer0 == answers[0]:
                print("correct")
        else:
            print("incorrect")
        elif choice == 1:
            print choice
            print options[1]
            answer1 = raw_input(inputs)
        if answer1 == answers[1]:
            print("correct")
        else:
            print("incorrect")
        elif choice == 2:
            print choice
            print options[2] 
            answer2 = raw_input(inputs)
        if answer2 == answers[2]:
            print("correct")
        else:
            print("incorrect")
        elif choice == 3:
            print choice
            print options[3]
            answer3 = raw_input(inputs)
        if answer3 == answers[3]:
            print("correct")
        else:
            print("incorrect")
        elif choice == 4:
            print choice
            print options[4]
            answer4 = raw_input(inputs)
        if answer4 == answers[4]:
            print("correct")
        else:
            print("incorrect")
        elif choice == 5:
            print choice
            print options[5]
            answer5 = raw_input(inputs)
        if answer5 == answers[5]:
            print("correct")
        else:
            print("incorrect")
 def main()
    date()
    quiz_infos()
    welcome()
    ask_questions()
main()

我想从列表中随机选择问题

我想知道一种从列表中随机选择问题的方法,如果该问题为 1:打印选项 1 和我的 raw_input(inputs) 同样适用于问题 2 3 4 等 idk 为什么我的代码实际上没有这样做并且只打印问题,所以如果 elif 函数不起作用! 我是 python 新手(编码新手),所以我可能肯定做错了什么, 通过变量[[[[输入=“你认为答案是什么?”]]]] 在此表示感谢! 代码是用python 2.7 idle 编写的

【问题讨论】:

  • 我认为您应该仔细查看缩进,然后发布代码的实际输出。您还混合了 python 2 和 3 打印语法。
  • 它没有任何问题或错误,它只是不打印列表中的我的选项,也不给我的 if elif 工作
  • 一个问题是您的elif choice == X: 语句在您的else: 之后,而它们应该在else: 语句之前。

标签: python python-3.x python-2.7


【解决方案1】:

有更简单的方法可以做到这一点,但这里是为 python 3 语法修改的带有适当缩进的确切代码(如果列表“问题”、“选项”和“答案”以及常量“输入”则有效)都是实际定义的,而不是空的)。

def ask_questions():
    choice = (random.choice(question))    
    if choice == 0:
        print(choice)
        print(options[0])
        answer0 = input(inputs)
        if answer0 == answers[0]:
                print("correct")
        else:
            print("incorrect")
    elif choice == 1:
        print(choice)
        print(options[1])
        answer1 = input(inputs)
        if answer1 == answers[1]:
            print("correct")
        else:
            print("incorrect")

等等……

C:\Users\me\Documents>python test.py
0
Question 1
User Input: Answer 1
correct
C:\Users\me\Documents>python test.py
4
Question 5
User Input: Answer 5
correct

正如我上面所说,如果您的代码中没有粘贴,那么您在提交的内容中会出现缩进错误。如果您的代码在打印问题后挂起,我怀疑您的“输入”可能是一个空字符串。

【讨论】:

    【解决方案2】:

    我个人会提出每个问题:在字典中回答

    Questions = {"What is your favorite Color?":"Blue","How many cats do I own?": "2"}
    

    然后您可以使用 KEYS 方法从列表中返回随机选择

    import random
    Questions = {"What is your favorite Color?":"Blue","How many cats do I own?": "2"}
    random.choice(Questions.keys())
    

    您可以在这里查找字典信息:Dictionary 还有随机模块Random

    【讨论】:

    • 你能说得更具体点吗?它的工作,但只是一点点帮助。我做了 question1 = random.choice 等。我打印了 question1。而不是我如何处理答案?我该如何打印?那么选项呢?也许如果 question1 打印 option1?如果我将选项放在列表中,那会起作用吗?有没有办法当随机打印出一些东西时,以后就不会再打印了?每次打印时都几乎要删除
    猜你喜欢
    • 2015-03-19
    • 1970-01-01
    • 2017-12-12
    • 2017-10-30
    • 2019-08-02
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2019-04-20
    相关资源
    最近更新 更多