## 22-Quiz Program Save scores to a text file.py.py

Sign up Free. Don't forget to check out our challenges, lessons, solve and learn series and more ...

## Code Snippet

``````#-------------------------------------------------------------------------------
# Name:       Maths Quiz
# Purpose:    Tutoral
# Created:     23/02/2016

#-------------------------------------------------------------------------------
def quiz():
import random
import csv

score=0
operators=("x", "+", "-")
valid_name=False
found_number=False
numbers="123456789"

#Test score file - connecting (w = write - this simply creates a scores text file)
#myFile=open("scores.txt","w")
#a = append. (add scores to - rather than over write what is already there)
with open("scores.txt","a") as f:

while valid_name==False:

found_number=False
name=input("Hi there, what is your name?: ")

for i in name:

for u in numbers:
if i==u:
found_number=True

if found_number==True:
valid_name=False
print("Invalid Name - Try again, without any numbers")

elif found_number==False:
valid_name=True
break

print("Great...let's start the quiz")
print("*********************************************")

for i in range(3):

num1=random.randint(5,10)
num2=random.randint(1,5)

operator=random.choice(operators)

if operator=="+":
elif operator=="-":
elif operator=="x":

print('What is ' + str(num1)+operator+str(num2))

print("That's Right")

score=score+1
else:
score=score
print("Well done:>" + name + "...Your Score was: " + str(score))

with open("scores.txt","a") as f:
#Open the file in append mode, and write to the file the name and score of student
f.write(name+ "," + str(score)+ "\n")

quiz()

``````