# Sports quiz in Python

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->"]

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:
print("Correct\n")
score = score + 1
else:
print("Incorrect\n")
if num == 2:
print("Correct\n")
score = score + 1
else:
print("Incorrect\n")
if num == 3:
print("Correct\n")
score = score + 1
else:
print("Incorrect\n")
if num == 4:
print("Correct\n")
score = score + 1
else:
print("Incorrect\n")
if num == 5:
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.

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:
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).

• Note: I don't usually write Python, so feel free to poke me if my syntax is off. – Ben N Jun 10 '16 at 20:42
• 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))) – Sisoma Munden Jun 10 '16 at 21:51