簡體   English   中英

用python腌制,寫入文件

[英]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()

這是數學測驗的一部分,我正在嘗試將分數寫入文本文件。 它說10G2上有語法錯誤?

所有變量都已定義,我已經導入了泡菜

提前致謝。 順便說一句,我正在做GCSE計算。

這是我的整個代碼:

#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()

嘗試更改此行

if  group == 10G2:  

至:

if  group == '10G2':  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM