# Python 2.7 quiz

I made this quiz but the code seems to be really lengthy. Can anyone find a way to make my code shorter? Considerably shorter would be best but anything helps. I think the main problem is the repeating of the questions.

choice=raw_input("What type of quiz do you want to do: maths or science? ")
topic=open("topic.txt", "a+")
topic.write(choice + '\n')
topic.close()
difficulty=raw_input("What difficulty do you want to play on: easy, medium or hard? ")
diff=open("difficulty.txt", "a+")
diff.write(difficulty + '\n')
diff.close()

score = 0

print("Well done")
score+=1
else:
raw_input("Press enter to continue")
return score

print("Well done")
score+=1
else:
raw_input("Press enter to continue")
return score

if choice.lower() == "maths" and difficulty.lower() == "easy":
easym=open("mathseasy.txt" , "r")
print lines[0]
print lines[1]
print("A. 4"+'\n'+"B. 6")
else:

print lines[2]
print("A. 5"+'\n'+"B. 6")
else:

print lines[3]
print("A. 15"+'\n'+"B. 20")
else:

print lines[4]
print("A. 13"+'\n'+"B. 15")
else:

print lines[5]
print("A. 100"+'\n'+"B. 110")
else:

if choice.lower() == "maths" and difficulty.lower() == "medium":
mediumm=open("mathsmedium.txt" , "r")
print lines[0]
print lines[1]
print("A. 30"+'\n'+"B. 35")
else:

print lines[2]
print("A. 100"+'\n'+"B. 110")
else:

print lines[3]
print("A. 13"+'\n'+"B. 15")
else:

print lines[4]
print("A. 30"+'\n'+"B. 32")
else:

print lines[5]
print("A. 21"+'\n'+"B. 29")
else:


Here is the data in mathseasy.txt:

Welcome to the easy maths quiz.
What's 2+2?
What's 11-6?
What's 5*4?
What's 26/2?
What's 11*10?


Here is the data in mathsmedium.txt:

Welcome to the medium maths quiz.
What's 5*6?
What's 79+31?
What's 26/2?
What's 4*8?
What's 50-21


## migrated from stackoverflow.comDec 15 '17 at 20:21

This question came from our site for professional and enthusiast programmers.

• A possibly more useful way of moving score around is to define it as a global variable because then you don't have to return it from your answer checkers or send it to them which will make your code more readable – 13ros27 Dec 15 '17 at 21:25
• It might make it easier to use if you stored the answers in the same text file as the questions - say alternating lines – 13ros27 Dec 16 '17 at 20:52

Here are some ways to shorten your code:

1. Your str_ and int_answercheck are basically the same function, except for one boolean expression. Instead of passing in the values and comparing them, why not write an underlying function that takes a boolean value and does the work:

def check_answer(was_correct):
if was_correct:
print("Well done")
score+=1
else:
raw_input("Press enter to continue")
return score


2. You wrote two answercheck functions, but you always use an if statement to determine which function to call. Put that logic into a single answercheck function:

def answercheck(user_answer, str_answer, int_answer):
else:

3. Instead of updating a global score, perform the addition if the answer check returns true. This doesn't make your code shorter, but it's good coding style (avoid global variables when not required):

if answercheck(user_answer, str_answer, int_answer):
score += 1

4. You're printing lines from the file, then printing allowable answers from the program source code. This means that the test file and the source code are in sync - they're not independent of each other. Since the source code is constant, why not put the possible answers in the test file:

Welcome to the easy maths quiz.
What's 2+2?
A. 4
B. 6
What's 11-6?
A. 5
B. 6


If you make these changes, you can convert this code:

print lines[0]
print lines[1]
print("A. 30"+'\n'+"B. 35")
else:


Into code like this:

header = lines[0]

print lines[1]    # question

score += 1

5. If only you had a way of reading the correct answers from the input file! Oh, wait! You do:

Welcome to the easy maths quiz.
What's 2+2?
A. 4
B. 6
What's 11-6?
A. 5
B. 6


By providing the answers in the file, you can make your code more general. (Note: There is an obvious looping solution. However, I don't see any loops in your code, so I'm assuming you haven't reached that stage in your learning.)

def question(num, lines):

i = num * 4 + 1

print lines[i]     # question
print lines[i + 1] # answer 1
print lines[i + 2] # answer 2



This will let you ask each question and return true/false if the answer is correct:

score = 0
if question(0, lines):
score += 1
if question(1, lines):
score += 1
if question(2, lines):
score += 1


(The looping version should be obvious, when you get there.)

6. You are handling the various degrees of difficulty as separate blocks of code. That was necessary since your code had the answers built in. But with the answers encoded in the questionnaire files, you no longer have to do that. Instead, just open the file and handle the questions in one place:

questions = subject.lower() + difficulty.lower() + ".txt"

with open(questions) as infile:

print lines[0]

score = 0
if question(0, lines):
score += 1
if question(1, lines):
score += 1
if question(2, lines):
score += 1

• thanks a lot, but i'm kinda confused. can you perhaps take a small section of my code and change it into the ways you told me to do it. i would really appreciate it! if you can't do it, please let me know so i can stop waiting for a response. thanks – Ibibat Dec 17 '17 at 19:01

A shorter way to do the answer checking is to do them both together:

def answercheck(score, user_answer, str_answer, int_answer):

NOTE: This will mean the rest of the code will have to be edited to just always send to answercheck