1
\$\begingroup\$

I'm a complete beginner at python and I know I'm probably approaching this the wrong way but I've tried making a quiz and can't work out how to make it more concise and work with longer lists of questions and answers without having to use lots of "if" statements. Here's the code:

import random

score = 0

questions = ["In which year did Diego Maradona score a goal with his hand?\n->",
             "In which sport can you win the Davis Cup?\n->",
             "How many minutes is a rugby match?\n->",
             "In which country were the first Olympic Games held?\n->",
             "In which country is the Interlagos F1 circuit?\n->"]
answers = ["1986","tennis","80","greece","brazil"]


print("---------------------------------")
print("You are running Ben's sports quiz")
print("---------------------------------\n")

question = random.sample(range(1,6),5)

for num in question:
    if num == 1:
        user_answer_1 = input(questions[0])
        if user_answer_1.lower() == answers[0]:
            print("Correct\n")
            score = score + 1
        else:
            print("Incorrect\n")
    if num == 2:
        user_answer_2 = input(questions[1])
        if user_answer_2.lower() == answers[1]:
            print("Correct\n")
            score = score + 1
        else:
            print("Incorrect\n")
    if num == 3:
        user_answer_3 = input(questions[2])
        if user_answer_3.lower() == answers[2]:
            print("Correct\n")
            score = score + 1
        else:
            print("Incorrect\n")
    if num == 4:
        user_answer_4 = input(questions[3])
        if user_answer_4.lower() == answers[3]:
            print("Correct\n")
            score = score + 1
        else:
            print("Incorrect\n")
    if num == 5:
        user_answer_5 = input(questions[4])
        if user_answer_5.lower() == answers[4]:
            print("Correct\n")
            score = score + 1
        else:
            print("Incorrect\n")

print("Your final score is",score,"out of 5\n")

input("Press ENTER to exit")

Any help would be greatly appreciated.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

It's great that you've put the questions and answers in arrays. You can take further advantage of that in your for loop. Notice how you're always looking at the place in the array that's indexed on less than num? You can use num - 1 as an array index. That eliminates all the duplication in your loop body.

for num in question:
    user_answer = input(questions[num - 1])
    if user_answer.lower() == answers[num - 1]:
        print("Correct\n")
        score += 1
    else:
        print("Incorrect\n")

As mentioned in the comments by Sisoma Munden, score = score + 1 can be turned into score += 1. This works for other arithmetic operators too (someVar *= 3 multiplies someVar by three).

You've also hardcoded the number of questions (in the random.sample line). You can get the length/count of a Python array by passing it to len. For instance, len(questions) is the number of items in the questions array. Therefore, you can replace the random.sample line with this:

question = random.sample(range(1, len(questions) + 1), len(questions))

Then your score-displaying line becomes this (I fixed the display as well, + concatenates strings together, the str function gets the text representation of a number):

print("Your final score is" + str(score) + " out of " + str(len(questions)) + "\n")

Come to think of it, I used len(questions) a lot there. You might consider declaring a variable for that after your arrays:

questionCount = len(questions)

You can then use that in place of len(questions).

\$\endgroup\$
2
  • \$\begingroup\$ Note: I don't usually write Python, so feel free to poke me if my syntax is off. \$\endgroup\$
    – Ben N
    Commented Jun 10, 2016 at 20:42
  • 1
    \$\begingroup\$ Also, when adding a value to a variable, you can say score += 1 instead of score = score + 1 In your answer print statement, you could use .format(): print("Your final score is {score} out of {questions} \n".format(score=score, questions=len(questions))) \$\endgroup\$ Commented Jun 10, 2016 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.