Making a quiz with shuffled questions

蹲街弑〆低调 提交于 2019-12-13 08:29:11

问题


I am wanting to join the Royal Air Force and thought as a good way to prepare I should code myself a quiz about their aircraft.

There are 28 aircraft that I have added to the quiz. For example - 'Where is the Typhoon FGR4 based?' And then I have 7 seconds to think before the options pop up for me to answer.

Instead of the quiz just going from the first to last question in the same order each time I would like it to be shuffled.

Here is the quiz in pastebin http://pastebin.com/wxVus42W

Also when I have answered the question I would like the console to clear itself for the next question to come up.

Can anyone help?

Thanks


回答1:


dont repeat yourself!, create methods, add your question and the excepted answer as flag

import time

def create_question(question = "", answer = "", excepted = ""):
    print (question)
    time.sleep(7)
    print(answer)

    while True:
        response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")

        if response == excepted:#CHANGE
            print ("Correct!\n")
            break
        else:
            print("Incorrect!!! Try again.")

            while True:
                response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")

                if response == excepted:#CHANGE
                    print ("Correct!\n")#CHANGE
                    stop = True
                    break
                else:
                    print("Incorrect!!! The Tornado GR4 is based at RAF Marham\n")#CHANGE
                    stop = True
                    break
            if stop:
                break

#first question               
create_question(question = "Where is the Tornado GR4 based?",
                answer = "a. RAF Marham\nb. RAF Conningsby\nc. RAF Waddington\nd. RAF Church Fenton\n",
                excepted = "a")
#second question                
create_question(question = "Where is the Typhoon FGR4 Based?",
                answer = "a. RAF Marham\nb. RAF Conningsby\nc. RAF Benson\nd. RAF Wyton\n",
                excepted = "b")



回答2:


What you want is to call the questions in whatever order you want.

Usually for something like this you will need to have your answers in a List or an Array. Then for each item in that list/array you show what you want to show. If you want to change the order of questions, you just shuffle the list/array. (Object Oriented Programming way of thinking)

But by the looks of it you don't load the questions into an list/array. So, the above will not work.



来源:https://stackoverflow.com/questions/40400192/making-a-quiz-with-shuffled-questions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!