Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quiz help
#1
Hi all,

Im currently trying to learn python in my spare time, im trying to create a quiz but i am having some trouble getting my las question to work correctly.

for a10_1 i can answer with any input and it will print as a correct answer, and for the 2nd and 3rd inputs it goes straight to printing "You cannot enter duplicate answers"

bonus question: how can i make the script re ask the question if the user does input a duplicate?

Thank you in advance and sorry if my explanation of the problem is poor, new to this :)

score = 0
print("Question 10")
print("Name the only 3 principalities (1 point per correct answer)")
a10_1 = input("What is your first answer?")
if a10_1.lower() == "andorra" or "liechtenstein" or "monaco":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
a10_2 = input("What is your second answer?")
if a10_2 == a10_1:
    print("You cannot enter duplicate answers")
elif a10_2.lower() == "andorra" or "liechtenstein" or "monaco":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
a10_3 = input("What is your third answer?")
if a10_3 == a10_1 or a10_2:
    print("You cannot enter duplicate answers")
elif a10_3.lower() == "andorra" or "liechtenstein" or "monaco":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")

print(score)
Yoriz write May-04-2021, 11:48 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
See Multiple expressions with "or" keyword and Validating User Input
Reply
#3
(May-04-2021, 11:49 AM)Yoriz Wrote: See Multiple expressions with "or" keyword and Validating User Input

hi, not sure how this helps me
Reply
#4
score = 0
print("Question 1")
a1 = input("How many countries does Germany border?")
if a1 == "9":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
print("Question 2")
a2 = input("What country is Tehran the capital of?")
if a2.lower() == "iran":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
print("Question 3")
a3 = input("Spain and Portugal are located on what peninsula?")
if a3.lower() == "iberia":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
print("Question 4")
a4 = input("How many countries did the USSR split into when it collapsed in 1991?")
if a4 == "15":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
print("Question 5")
a5 = input("Which german state is mainly responsible for german unification?")
if a5.lower() == "prussia":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
print("Question 6")
a6 = input("Name one of the 3 caucasus countries")
if a6.lower() == "georgia" or "armenia" or "azerbaijan":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
print("Question 7")
a7 = input("What is the name of the mountain range located in the north west of the North American continent?")
if a7.lower() == "rockies":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
print("Question 8")
a8 = input("What is the name of the country which is completely enclaved by South Africa")
if a8.lower() == "lesotho":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
print("Question 9")
a9 = input("In what country is the Kamchatka peninsula located")
if a9.lower() == "russia":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
print("Question 10")
print("Name the only 3 principalities (1 point per correct answer)")
a10_1 = input("What is your first answer?")
if a10_1.lower() == "andorra" and "liechtenstein" and "monaco":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
a10_2 = input("What is your second answer?")
if a10_2 == a10_1:
    print("You cannot enter duplicate answers")
elif a10_2.lower() == "andorra" or "liechtenstein" or "monaco":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
a10_3 = input("What is your third answer?")
if a10_3 == a10_1 or a10_2:
    print("You cannot enter duplicate answers")
elif a10_3.lower() == "andorra" or "liechtenstein" or "monaco":
    print("Correct, 1 point added!")
    score += 1
else:
    print("Incorrect")
print("You scored ", score ,"/12!")
Yoriz write May-04-2021, 12:28 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#5
(May-04-2021, 12:08 PM)jslack17 Wrote:
(May-04-2021, 11:49 AM)Yoriz Wrote: See Multiple expressions with "or" keyword and Validating User Input

hi, not sure how this helps me
If you take the time to read them you will see how they relate to your problem and your bonus question.
Reply
#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


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 793 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