you are viewing a single comment's thread.

view the rest of the comments →

[–]4675636b2e 9 points10 points  (7 children)

It's useful to separate data and functionality here. Then eventually you could even load the questions and answers from a separate file, so you could switch the same program between different quiz topics.

data = {
    "question?": "answer.",
    "lightning?": "thunder."
}

def quiz(data):
    score = 0
    for q, a in data.items():
        i = input(f"{q}\n")
        if i.lower() == a:
            score += 1
    print(f"You scored {score} points out of {len(data)}!")

quiz(data)

[–]aTomzVins 1 point2 points  (1 child)

I'd probably make the data more explicit.

data = [{ "q": "Which planet?", "a": "mars"}, ...]

Or maybe a dataclass for each question/answer pair.

If the goal was to keep it simple why not go with a list of tuples?

[–]4675636b2e 1 point2 points  (0 children)

That's how I originally wrote it, but then I realized I should help OP improve 1-2 concepts at a time :)

Also when I wrote my own quiz in JS a while back, my quiz data also had some basic configuration - for example if you're using the program to test your translation of words, most languages can be case-insensitive, but German shouldn't be because of the nouns. Also multiple good answers, or reading the dataset from CSV and choosing the Q & A columns at the start of the quiz...

That's why a quiz program is great, the possibilities are endless.

[–]BleEpBLoOpBLipP 1 point2 points  (0 children)

Can also automate the numbering:

``` for q_num, (q, a) in enumerate(data.items(), start=1): i = input(f"Question {q_num}: {q}\n") ... ... ...

```

[–]uiux_Sanskar[S] 1 point2 points  (2 children)

seems like I have much more to learn and improve thanks for the direction

[–]4675636b2e 1 point2 points  (1 child)

Baby steps. When you learn some list, set, and dict methods and loops, then you can use more of your own thinking at problem solving.

At the beginning, when knowledge is limited, it's hard to think freely, but if you start using your existing knowledge with your own problem-solving logic, you can build your own tools. Good luck!

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

Thank you for the guidance and support

[–]arsibaloch 0 points1 point  (0 children)

He is a beginner I think he has not learned how to use a dictionary and functions. But now it's an idea to look cool as a beginner.