简体   繁体   中英

Python: Creating a multiple choice quiz

I'm trying to write a program in python that will first, ask the user to choose a sport (football, tennis, golf or badminton) and then pick either easy, medium or hard. The program should then ask the user 5 questions about the sport with 4 choices if the user has picked 'easy', 6 if the user has picked 'medium' and 8 if the user has picked 'hard'.

Can anyone help me start this off?

There's a Python library called tkinter which you could use for this. A basic design for the selection process is below:

from tkinter import *

class App:
    def __init__(self, root):
        self.root = root
        self.x = IntVar()
        self.y = IntVar()
        self.frame = Frame(root)
        self.frame.pack()
        self.sport = [("football", 0), ("tennis", 1), ("golf", 2), ("badminton", 3)]
        self.diff = [("easy", 0), ("medium", 1), ("hard", 2)]
        for i, c in self.sport:
            Radiobutton(self.frame, text=i, variable=self.x, value=c, indicatoron=0).pack(anchor="w", fill="both", expand=True)
        for i, c in self.diff:
            Radiobutton(self.frame, text=i, variable=self.y, value=c, indicatoron=0).pack(anchor="w", fill="both", expand=True)
        Button(self.frame, text="Ok", command=self.start).pack(anchor="w", fill="both", expand=True)
    def start(self):
        print("questions: "+self.sport[self.x.get()][0]+", "+"difficulty: "+self.diff[self.y.get()][0])


root = Tk()
App(root)
root.mainloop()

From there you'd need to start your loop and cycle through a selection of questions which would most likely be contained in some form of nested iterable.

You could have something like:

[[(fb question 1, [list of answers]), ...], [(tennis question 1, [list of answers]), ...], ...]

On a side note, Stack Overflow is not a free programming service, nor a resource for creating homework assignments for you. We are more than happy to help you out with problems if you get stuck at some point in your development cycle, but will not write the program for you.

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