简体   繁体   中英

How do I loop through a quiz using lists five times in Python?

I am working on building a quiz that incorporates an API. I want to make a quiz bank with a lot of questions but only want it to ask 5 questions at a time. Here is the code I have currently.

question_prompts = [
        "What day in 1999 was the aibo ERS 110 released?\n(a) May 11\n(b) January 23\n(c) October 31\n(d)December 15\n\n",
        "What is the official color name of the 4th anniversary limited release aibo ERS 210?\n(a) Everest White\n(b) Sapphire Blue\n(c) Cyber Blue\n(d) Holiday Red\n\n",
        "What toy was released specifically for the aibo ERS 1000?\n(a) Aibone\n(b) Pink Ball\n(c) Dice\n(d) Fire Hydrant\n\n",
        "What aibo software was the first to recognize faces?\n(a) Life 2\n(b) Mind 1\n(c) Kawaii\n(d) Recognition\n\n",
        "What charity did the proceeds from the Gold Quarts ERS 1000 sales go to?\n(a) Unicef\n(b) World Wildlife Fund\n(c)March for Dimes\n(d) Red Cross\n\n",
        "What does the word aibo mean in Japanese?\n(a) dog\n(b) robot\n(c) entertainment\n(d) pal\n\n",
        "What alcoholic beverage company did Sony partner with to give away exclusive latte aibos with colorful ears?\n(a) Suntory\n(b) Kirin\n(c) Sapporo\n(d) Asahi\n\n",
        "How many years had aibo been discontinued prior to the release of the ERS 1000?\n(a) 10\n(b) 11\n(c) 12\n(d) 15\n\n"
        ]

questions = [
        Question(question_prompts[0], "a"),
        Question(question_prompts[1], "c"),
        Question(question_prompts[2], "c"),
        Question(question_prompts[3], "d"),
        Question(question_prompts[4], "b"),
        Question(question_prompts[5], "d"),
        Question(question_prompts[6], "a"),
        Question(question_prompts[7], "c")
        ]
random.shuffle(questions)
def run_test(questions):
    score = 0
    for question in questions:
        answer = input(question.prompt)
        if answer == question.answer:
            score += 1
            do_action("play_motion", '{"Category":"nodHead","Mode":"NONE"}') 
        else:
            do_action("play_motion", '{"Category":"shakeHead","Mode":"NONE"}')
    print("You got " + str(score) + "/" + str(len(questions)) + "correct")

run_test(questions)

Right now, the quiz will run through every question in randomized order. I want it to stop and calculate the score after 5 questions so this doesn't go on forever. I'm guessing I need to change something in the for loop but not sure the right way to make it only go through 5 times and then stop. I appreciate help as a new programmer. Thank you!

Use the inbuilt sample function to sample your list into a smaller list:

from random import sample

sampled_q = sample(questions,5)

Then just loop through that:

for question in sampled_q:
    ...

Write this before your for loop and change it -

    no_of_ques = 0
    for question in questions:
        no_of_ques += 1
        answer = input(question.prompt)
        if answer == question.answer:
            score += 1
            do_action("play_motion", '{"Category":"nodHead","Mode":"NONE"}')
        else:
            do_action("play_motion", '{"Category":"shakeHead","Mode":"NONE"}')

        if no_of_ques == 5: 
           print("You got " + str(score) + "/" + str(len(questions)) + "correct")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM