Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quiz help
#6
This should be solved using function, but I feel that this would be too big step in learning process, so more beginner friendly approach.

We should try to keep our code DRY (Don't Repeat Yourself) and in order to do so we should keep questions and answers in some datastructure. There are several possibilities but I will continue with dictionary. This allows us iterate over question-answer pairs and not rewrite code every time number of questions changes.

There are three cases/types of question-answers:

- type-1: one question - one correct answer, to gain point user response must be exact match;
- type-2: one question - several correct answers, to gain point user response must be exact match of one in answers
- type-3: one question repeatedly - several answers, to gain point user response must be exact match of one in answers, only once, repeated as many times as there are different answers

Quite obviously 'low-hanging' fruits are two first options, so we start writing code which handles these. We keep answers in tuple, even if there is only one answer as this way we can have same code for both type-1 and type-2 questions:

# creating question - answer mapping

quiz = {'How many countries does Germany border? ': ('9'),
        'What country is Tehran the capital of? ': ('iran'),
        'Name one of the 3 caucasus countries ': ("georgia", "armenia",
                                                 "azerbaijan"),
        'Name the only 3 principalities (1 point per correct answer) ':('andorra', 'liechtenstein', 'monaco')}

# iterate over questions and add point if answer is correct

points = 0
 
for question, answer in quiz.items():
    from_user = input(question).lower()
    if from_user in answer:
        points += 1

print(f'Total number of points is {points}')
Simple enough. Last question is not functioning as required (adds only one point) but otherwise should be ok. We can make it little bit more informative for user by adding question number using enumerate:

points = 0

for i, (question, answer) in enumerate(quiz.items(), start=1):
    from_user = input(f'{i}. {question}').lower()
    if from_user in answer:
        points += 1

print(f'Total number of points is {points}')
Now we should think how to approach situation where we award point for every correct answer which is not repetition of some earlier answer.

What we need to solve:

- how to distinguish type-3 questions from others?
- how we repeat question and how many times
- how we know that this answer is not already provided

How to distinguish third type of question where several points can be earned? I go for part of question '(1 point per correct answer)'. If this in question then it's type-3 question.

How to repeat and how many times. Python has for-loop for repetition and we must repeat if once for every answer the question has.

In order to know whether answer is already provided or not we should keep them in some mutable datastructure (as we need add items into it based on responses).

So we add logic what deals with type-3 to existing code:

points = 0

trigger = '(1 point per correct answer)'

for i, (question, answer) in enumerate(quiz.items(), start=1):
    if trigger in question:
        responses = []
        for j, _ in enumerate(answer, start=1):
            from_user = input(f'{i}.{j} {question}').lower()
            if from_user in responses:
                continue
            if from_user in answer:
                points += 1
                responses.append(from_user)
    else:
        from_user = input(f'{i}. {question}').lower()
        if from_user in answer:
            points += 1

print(f'Total number of points is {points}')
One can immediately see that there are still repetitions. There is also lurking gotcha waiting to happen (we need to clear responses after every question otherwise we may encounter answer collision)

I think that this is simple walkthrough how can it be solved. Now one should think how to make it totally DRY and refactor it with using function(s).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Quiz help - by jslack17 - May-04-2021, 11:38 AM
RE: Quiz help - by Yoriz - May-04-2021, 11:49 AM
RE: Quiz help - by jslack17 - May-04-2021, 12:08 PM
RE: Quiz help - by Yoriz - May-04-2021, 12:31 PM
RE: Quiz help - by jslack17 - May-04-2021, 12:09 PM
RE: Quiz help - by perfringo - May-04-2021, 02:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  QUIZ GUI Form_When admin panel is open, main quiz form is getting freeze Uday 4 806 Aug-25-2023, 08:24 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020