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
def str_answercheck(score, user_answer, str_answer):
if user_answer.upper()==str_answer:
print("Well done")
score+=1
else:
print ("Wrong, it was "+str_answer)
raw_input("Press enter to continue")
return score
def int_answercheck(score, user_answer, int_answer):
if int(user_answer.upper())==int_answer:
print("Well done")
score+=1
else:
print ("Wrong, it was "+str(int_answer))
raw_input("Press enter to continue")
return score
if choice.lower() == "maths" and difficulty.lower() == "easy":
easym=open("mathseasy.txt" , "r")
lines = easym.readlines()
print lines[0]
print lines[1]
print("A. 4"+'\n'+"B. 6")
str_answer_one="A"
int_answer_one=4
user_answer_one=raw_input("Select your answer. ")
if user_answer_one.isdigit()==True:
first_score=int_answercheck(score, user_answer_one, int_answer_one)
else:
first_score=str_answercheck(score, user_answer_one, str_answer_one)
print lines[2]
print("A. 5"+'\n'+"B. 6")
str_answer_two="A"
int_answer_two=5
user_answer_two=raw_input("Select your answer. ")
if user_answer_two.isdigit()==True:
second_score=int_answercheck(first_score, user_answer_two, int_answer_two)
else:
second_score=str_answercheck(first_score, user_answer_two, str_answer_two)
print lines[3]
print("A. 15"+'\n'+"B. 20")
str_answer_three="B"
int_answer_three=20
user_answer_three=raw_input("Select your answer. ")
if user_answer_three.isdigit()==True:
third_score=int_answercheck(second_score, user_answer_three, int_answer_three)
else:
third_score=str_answercheck(second_score, user_answer_three, str_answer_three)
print lines[4]
print("A. 13"+'\n'+"B. 15")
str_answer_four="A"
int_answer_four=13
user_answer_four=raw_input("Select your answer. ")
if user_answer_four.isdigit()==True:
fourth_score=int_answercheck(third_score, user_answer_four, int_answer_four)
else:
fourth_score=str_answercheck(third_score, user_answer_four, str_answer_four)
print lines[5]
print("A. 100"+'\n'+"B. 110")
str_answer_five="B"
int_answer_five=110
user_answer_five=raw_input("Select your answer. ")
if user_answer_five.isdigit()==True:
fifth_score=int_answercheck(fourth_score, user_answer_five, int_answer_five)
else:
fifth_score=str_answercheck(fourth_score, user_answer_five, str_answer_five)
if choice.lower() == "maths" and difficulty.lower() == "medium":
mediumm=open("mathsmedium.txt" , "r")
lines = mediumm.readlines()
print lines[0]
print lines[1]
print("A. 30"+'\n'+"B. 35")
str_answer_one="A"
int_answer_one=30
user_answer_one=raw_input("Select your answer. ")
if user_answer_one.isdigit()==True:
first_score=int_answercheck(score, user_answer_one, int_answer_one)
else:
first_score=str_answercheck(score, user_answer_one, str_answer_one)
print lines[2]
print("A. 100"+'\n'+"B. 110")
str_answer_two="B"
int_answer_two=110
user_answer_two=raw_input("Select your answer. ")
if user_answer_two.isdigit()==True:
second_score=int_answercheck(first_score, user_answer_two, int_answer_two)
else:
second_score=str_answercheck(first_score, user_answer_two, str_answer_two)
print lines[3]
print("A. 13"+'\n'+"B. 15")
str_answer_three="A"
int_answer_three=13
user_answer_three=raw_input("Select your answer. ")
if user_answer_three.isdigit()==True:
third_score=int_answercheck(second_score, user_answer_three, int_answer_three)
else:
third_score=str_answercheck(second_score, user_answer_three, str_answer_three)
print lines[4]
print("A. 30"+'\n'+"B. 32")
str_answer_four="B"
int_answer_four=32
user_answer_four=raw_input("Select your answer. ")
if user_answer_four.isdigit()==True:
fourth_score=int_answercheck(third_score, user_answer_four, int_answer_four)
else:
fourth_score=str_answercheck(third_score, user_answer_four, str_answer_four)
print lines[5]
print("A. 21"+'\n'+"B. 29")
str_answer_five="B"
int_answer_five=29
user_answer_five=raw_input("Select your answer. ")
if user_answer_five.isdigit()==True:
fifth_score=int_answercheck(fourth_score, user_answer_five, int_answer_five)
else:
fifth_score=str_answercheck(fourth_score, user_answer_five, str_answer_five)
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