简体   繁体   中英

Pickle in python, writing to files

if  group == 10G2:
file_name='10G2 Scores.txt'
fileObject = open(file_Name,'wb')
pickle.dump(+str(name) + ' got ' + str(finalscore) + ' percent from ' + str(totalquestions) + ' question(s), this is ' + str(score) + ' out of ' + str(totalquestions) + '.') ,fileObject)
fileObject.close()

This is part of a maths quiz and I am trying to write scores to a text file. It says there is a syntax error on the 10G2?

All of the variables have been defined and I have imported pickle

Thanks in advance. BTW, I'm doing GCSE computing.

This is my whole code:

#Maths Quiz

noq=0

score=0

import math

import pickle

import random

import time

import pickle

def write(name, finalscore, totalquestions, score, file_Object):
    pickle.dump ((str(name) + ' got ' + str(finalscore) + ' percent from ' + str(totalquestions) + ' question(s), this is ' + str(score) + ' out of ' + str(totalquestions) + '.') ,file_Object)
return 

group=input("What mentor group are you in?:     ")

name=input("What is your name?:     ")

print ("Hello " + name + ". How many questions would you like to do?:")

totalquestions=int(input("                                                   "))

for i in range (0,(totalquestions)):
    noq=noq+1
    print ("Question " +str(noq))
    numberone=random.randint(1, 10)
    numbertwo=random.randint(1, 10)
    answer=int(numberone) + int(numbertwo)
    print ("What is " +str(numberone) + "+" + str(numbertwo) + "?:")
    uanswer=int(input(""))
    if answer==uanswer:
        print("That is correct!!!")
        score=score +1
        print ("Your score is.....")
        time.sleep(0.5)
        print("" +str(score) + " out of " + str(i+1))
        time.sleep(1)
    else:
        print ("Sorry, that is wrong!!!")
        score=score
        print ("Your score is.....")
        time.sleep(0.5)
        print ("" +str(score) + " out of " + str(i+1))
        time.sleep(1)

time.sleep(0.5)
print ("Your final score is.....")

finalscore=score/totalquestions
finalscore=finalscore*100
time.sleep(3)
print ("" +str(finalscore)+ " percent")

#file write###############################################################################

if group == 10G2:
    file_name='10G2 Scores.txt'
    fileObject= open(file_Name,'w')
    pickle.dump((+str(name) + ' got ' + str(finalscore) + ' percent from ' + str(totalquestions) + ' question(s), this is ' + str(score) + ' out of ' + str(totalquestions) + '.') ,fileObject)
    fileObject.close()

elif group == 10G1:
    file_name='10G1 Scores.txt'
    fileObject = open(file_Name,'w')
    pickle.dump((+str(name) + ' got ' + str(finalscore) + ' percent from ' + str(totalquestions) + ' question(s), this is ' + str(score) + ' out of ' + str(totalquestions) + '.') ,fileObject)
    fileObject.close()

elif group == 10G3:
    file_name='10G3 Scores.txt'
    fileObject = open(file_Name,'w')
    pickle.dump((+str(name) + ' got ' + str(finalscore) + ' percent from ' + str(totalquestions) + ' question(s), this is ' + str(score) + ' out of ' + str(totalquestions) + '.') ,fileObject)
    fileObject.close()

elif group == 10G4:
    file_name='10G4 Scores.txt'
    fileObject = open(file_Name,'w')
    write ()
    fileObject.close()

elif group == 10G5:
    file_name='10G5 Scores.txt'
    fileObject = open(file_Name,'w')
    write ()
    fileObject.close()

elif group == 10G6:
    file_name='10G6 Scores.txt'
    fileObject = open(file_Name,'w')
    write ()
    fileObject.close()

elif group == 10G7:
    file-name='10G7 Scores.txt'
    fileObject = open(file_Name,'w')
    write ()
    fileObject.close()

Try changing this line

if  group == 10G2:  

to:

if  group == '10G2':  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM