Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

python - Making a quiz with shuffled questions

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
")

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

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...