dimanche 1 février 2015

Python doesn't write information correctly to a file


My program should save the data of each student who takes a test to a file. There are 3 files for 3 different classes and when the quiz has been completed and python writes to the files the information isn't completely correct please can someone help. The problem is that the score that the person got isnt shown correctly and how do you put the name on a different line in the file. I tried using '\n' but it doesnt work. Any help would be appreciated


This is the code (sorry about the formatting)




import random

def Quiz():

name=input("What is your name?\n")

print('Hello ' +name)

mathoperators = ['*','-','+']

count=0

Score=0

for i in range (0, 10):

operator=random.choice(mathoperators)

number1=random.randint(1,10)

number2= random.randint(1,10)

answer=input(str(number1)+str(operator)+str(number2)+"=")

if operator == '+' :

count = number1+number2




elif operator == '-' :
count = number1-number2

elif operator =='*' :
count = number1*number2
if count == int (answer) :
print ('Correct')
Score += 1
else:
print('Incorrect')

print('your final score is '+str(Score)+" out of 10")


Quiz()


name=input('What is your name?')

classes=input('Are you in class 1, 2 or 3?')

Score=0

count=0

if classes=='1':

Class1=open('Class1.txt', 'a')

Class1.write(name)

Class1.write(str(Score))

Class1.write('Out of 10\n')

Class1.close()


elif classes=='2':

Class2=open('Class2.txt', 'a')

Class2.write(str(name))

Class2.write(str(Score))

Class2.write('Out of 10\n')

Class2.close()


elif classes=='3':

Class3=open('Class3.txt', 'a')

Class3.write(str(name))

Class3.write(str(Score))

Class3.write('Out of 10\n')

Class3.close()



Here is the python output


What is your name?

bob

Hello bob

8-8=0

Correct

your final score is 1 out of 10

10*6=60

Correct

your final score is 2 out of 10

4+2=6

Correct

your final score is 3 out of 10 8-5=2

Incorrect

your final score is 3 out of 10

3-7=-4

Correct

your final score is 4 out of 10

9-7=2

Correct

your final score is 5 out of 10

7-7=0

Correct

your final score is 6 out of 10

4-5=1

Incorrect

your final score is 6 out of 10

7*2=14

Correct

your final score is 7 out of 10

3-9=-6

Correct

your final score is 8 out of 10

What is your name?bob

Are you in class 1, 2 or 3?2


Finally here is what appears in the notepad file

bob0Out of 10


The score should be on a separate line to the name and the score is incorrect as it should be 8. Grateful of any help





Aucun commentaire:

Enregistrer un commentaire