-3

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 Azikwe\n: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) Ikorodu\n(b) Ikeja\n(c) Surulere\n(d) Victoria Island\nYour 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) Abuja\n(c) Lagos\n(d) Calabar\nYour 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 Fashola\n(b)Akinwunmi Ambode\n(c) Ahmed Tinubu\n (d) Lateef Jakande\nYour 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) Etisalat\n(b) Airtel\n(c) MTN\n(d) Globacoms\n 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.

  • 1
    Consider giving a [mcve] rather than dumping all your code... – Julien Aug 27 '18 at 02:09
  • To amplify a bit on what went wrong here (and how a MCVE would lead to a stronger question) -- our goal here is to build a collections of questions and answers that are helpful to as many people as possible. That means that every question should be written to focus on the problem it includes, with unrelated details left out; posting only the shortest code that creates that problem makes it easier to see the error, and thus easier to understand how an answer fixes it, so someone else making the same mistake can more easily see the similarity and learn from your question's answers. – Charles Duffy Aug 27 '18 at 02:20
  • ...similarly, an ideal title focuses on the problem you encountered, not the thing you were trying to accomplish when you encountered it -- since it's the problem that someone else whom an answer is useful to will be searching for; the next person to have the same mistake in understanding how `or` works probably won't be also working on a quiz program themselves. – Charles Duffy Aug 27 '18 at 02:26

1 Answers1

1

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")
Ashish Acharya
  • 3,349
  • 1
  • 16
  • 25
  • Thank you Ashish, for the top section, the lower section that deals with redundancy is still way over my head as i am just learning. But thanks for the response as it worked out fine. – Adenuga Olaolu Aug 30 '18 at 00:55