I intend to write a quiz program that adds up points when the answer is correct and deducts point when a wrong answer is given in python

Question:

This is what i came up with, and i cant understand why what is going on, as my expectation is to have point increment for every correct answer and point deduction for any wrong answer.

name = str(input("What is your name?: "))
score = 0

answer1 = str(input("(1) What is the name of the first president of Nigeria? n(a) Amadu Bello n(b) Shehu Shagari n(c) Olusegun Obasanjo n(d) Nnamdi Azikwen:Your answer: "))

if answer1 == "d" or "D" or "Nnamdi" or "Nnamdi Azikwe" and "Azikwe":
    score = score+5
    print ( "Weldone", name, "you've scored", score, "points")
else:
    score = score - 5
    print( "ouch!!!", name, "you missed that, you've scored, ", score, "points")


answer2 = str(input("(2) What is the name of the capital of Lagos state? n(a) Ikorodun(b) Ikejan(c) Suruleren(d) Victoria IslandnYour answer: "))

if answer2 == "a" or "A" or "Ikeja":
    score = score+5
    print ( "Weldone", name, "you've scored", score, "points")
else:
    score = score - 5
    print( "ouch!!!", name, "you missed that, you've scored, ", score, "points")


answer3 = str(input("(3) What is the name of the first capital of Nigeria?n(a) Kano n(b) Abujan(c) Lagosn(d) CalabarnYour Answer: "))

if answer3 == "c" or "C" or "lagos" and "Lagos":
    score = score+5
    print ( "Weldone", name, "you've scored", score, "points")
else:
    score = score - 5
    print( "ouch!!!", name, "you missed that, you've scored, ", score, "points")


answer4 = str(input("(4) What is the name of the governor of Lagos when Code-Lagos was first implemented? n(a)Raji Fasholan(b)Akinwunmi Amboden(c) Ahmed Tinubun (d) Lateef JakandenYour answer: "))

if answer4 == "b" or "B" or "Ambode" or "Akinwunmi Ambode" or "akinwunmi ambode":
    score = score+5
    print ( "Weldone", name, "you've scored", score, "points")
else:
    score = score - 5
    print( "ouch!!!", name, "you missed that, you've scored, ", score, "points")


answer5 = str(input("(5) Which of the following is indigenous Nigerian content? n(a) Etisalatn(b) Airteln(c) MTNn(d) Globacomsn Your Answer: "))

if answer5 == "d" or "D" or "glo" or "Glo" or "GLO":
    score = score+5
    print ( "Weldone", name, "you've scored", score, "points")
else:
    score = score - 5
    print( "ouch!!!", name, "you missed that, you've scored, ", score, "points")

The output, just accepts any answer as correct and adds up point.

Asked By: Adenuga Olaolu

||

Answers:

The problem in your code is that you’re using the or operator wrongly in the if condition.

In general, if you want to check the variable a against values B and C, you would need to do:

if a == B or a == C:

as opposed to simply

if a == B or C

Notice how in the second case, it’s taking the value of B or C which is a boolean and comparing it with a, which is obviously not what you want.

If you want to compare a against a whole list of variables, you could do:

if a in [B, C, D, ...]:

So, in your code, try replacing this:

if answer1 == "d" or "D" or "Nnamdi" or "Nnamdi Azikwe" and "Azikwe":

with this:

if answer1 in ["d", "D", "Nnamdi", "Nnamdi Azikwe", "Azikwe"]:

…and so on

You could vastly remove redundancy in your code by using a dictionary to store your questions, options, and answers, like this:

questions = [
    {
        "question": "What is the name of the first president of Nigeria?",
        "options": ["Amadu Bello", "Shehu Shagari", "Olusegun Obasanjo", "Nnamdi Azikwe"],
        "answers": ["d", "nnamdi", "nnamdi azikwe", "azikwe"]
    },
# add more questions here in the same format as above
]

name = str(input("What is your name?: "))
score = 0

for q_number, q in enumerate(questions):
    question_text = '({}) {}n'.format(q_number + 1, q["question"])
    options_text = 'n'.join("({}) {}".format(chr(i + 65), qsn) for i, qsn in enumerate(q["options"])) + "n"
    display_text = question_text + options_text

    # answer is converted to lower case to make comparison with options easier
    answer = str(input(display_text)).lower()

    if answer in q["answers"]:
        score += 5
        print ("Well done", name, "you've scored", score, "points")
    else:
        score -= 5
        print("ouch!!!", name, "you missed that, you've scored, ", score, "points")
Answered By: Ashish Acharya
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.