I am trying to make a quiz which then stores the score of the user in an excel file however when I do this it writes all the scores that the user got through out the 10 question quiz - it gives me the score after each question but all I want is the name of the user and their end score to be put into the file. How would I do this in my code below?
import time
import random
question = 0
score = 0
name = input("What is your full name?")
while True:
studentclass = input("What is your class name? Please enter 1, 2 or 3.")
if studentclass.lower() not in ('1', '2', '3'):
print("Not an appropriate choice.")
else:
break
print ("Hello " + name, "welcome to The Arithmetic Quiz. This quiz is for eleven year olds. Use integers to enter the answer!")
time.sleep(2)
operands1 = list(range(2, 12))
operators = ["+","-","x"]
operands2 = list(range(2, 12))
while question < 10:
operand1 = random.choice(operands1)
operand2 = random.choice(operands2)
operator = random.choice(operators)
def inputNumber(message):
while True:
try:
userInput = int(input(message))
except ValueError:
print("Not an integer! Try again.")
continue
else:
return userInput
break
user_answer =int(inputNumber('{} {} {} = '.format(operand1, operator, operand2)))
if operator == '+':
expected_answer = operand1 + operand2
if user_answer==expected_answer:
print('This is correct!')
score = score + 1
question = question + 1
time.sleep(2)
else:
print('This is incorrect!')
question = question + 1
time.sleep(2)
if operator == '-':
expected_answer = operand1 - operand2
if user_answer==expected_answer:
print('This is correct!')
score = score + 1
question = question + 1
time.sleep(2)
else:
print('This is incorrect!')
question = question + 1
time.sleep(2)
if operator == 'x':
expected_answer = operand1 * operand2
if user_answer==expected_answer:
print('This is correct!')
score = score + 1
question = question + 1
time.sleep(2)
else:
print('This is incorrect!')
question = question + 1
time.sleep(2)
if question==10:
endscore = str(score)
print ("Your score is {} out of 10".format(score))
if studentclass == "1":
text_file = open("groupone.csv", "a")
text_file.write (name + "," + (str(score)+ "\n"))
text_file.close()
elif studentclass == "2":
text_file = open("grouptwo.csv", "a")
text_file.write(name + "," + (str(score)+ "\n"))
text_file.close()
else:
text_file = open("groupthree.csv", "a")
text_file.write(name + "," + (str(score)+ "\n"))
text_file.close()
It's printing score for each question since your condition to write into a csv file, does not require the number of questions to be 10. Indentation should do it:
if question==10:
endscore = str(score)
print ("Your score is {} out of 10".format(score))
# Below are indented, meaning they are included UNDER the if statement above
if studentclass == "1":
text_file = open("groupone.csv", "a")
text_file.write (name + "," + (str(score)+ "\n"))
text_file.close()
elif studentclass == "2":
text_file = open("grouptwo.csv", "a")
text_file.write(name + "," + (str(score)+ "\n"))
text_file.close()
else:
text_file = open("groupthree.csv", "a")
text_file.write(name + "," + (str(score)+ "\n"))
text_file.close()
Doing this implies that you will be writing to an output csv file only when questions have reached 10.