保存为1级,2级和3级的csv

时间:2016-02-18 09:45:10

标签: python

我有这个程序

# Controlled Assessment - Basic Times Table Test 
import random 
import csv

score = 0

print ("Welcome to the times table test")

name = input("Please type your name: ") 
classnumber = input ("What is your class number?")

print ("How to play") 
print ("Step 1: When you see a question work out the answer and type it in the space.") 
print ("Step 2: Once you have typed your answer press the enter key.") 
print ("Step 3: The program will tell you if you're right or wrong.") 
print ("Step 4: The next question will load and you can repeat from step 1.") 
print ("When you have answered all 10 questions your final score will be printed.")

for q in range(10):
    Number1 = random.randint(1,12)
    Number2 = random.randint(1,12)
    ListOfOperator = ['+','-','*']
    Operator =random.choice(ListOfOperator)
    print ('what is' ,Number1,Operator,Number2)
    Answer= int(input ("Please Type Your Answer: "))

    questionanswer = (Number1,Operator,Number2)

    if Operator == '+':
        realanswer = Number1+Number2
    elif Operator == '-':
        realanswer = Number1-Number2
    elif Operator == '*':
        realanswer = Number1*Number2


    if Answer==realanswer:
        print("Your answer is correct")
        score = score + 1
        print (score)
    else:
        print("Your answer is incorrect, the correct answer is.",realanswer,".")
        print (score)

目前,我有以下代码可以使用此人的姓名保存乐谱。

    with open ('class 1', 'a') as Class1:
    Class1Writer=csv.writer(Class1)
    Class1Writer.writerow ([name, classnumber])
Class1.close()

但是这会保存到新文件而不是已创建的Class1,2或3文件,如何修改“with open”代码以便保存到csv文件?

  with open ('Class3', 'a') as Class3:
     Class3Writer=csv.writer(Class3)
     Class3Writer.writerow ([name, classnumber])
 Class3.close()

我现在已经为输入添加了if语句,但这是说没有定义class 2和class 3.

  

如果classnumber ==(“1”):       使用open('Class1','a')作为Class1:           Class1Writer = csv.writer(1类)           Class1Writer.writerow([name,classnumber])Class1.close()

     

else classnumber ==(“2”):       使用open('Class2','a')作为Class2:           Class2Writer = csv.writer(等级2)           Class2Writer.writerow([name,classnumber])Class2.close()

     

else classnumber ==(“3”):       使用open('Class3','a')作为Class3:           Class3Writer = csv.writer(Class3的)           Class3Writer.writerow([name,classnumber])Class3.close()

1 个答案:

答案 0 :(得分:0)

fieldnames=['name','classnumber','score']

if os.path.exists('class1.csv'):
    with open('class1.csv', "ab") as csv_file:
        Class1Writer = csv.DictWriter(csv_file, delimiter=',',dialect='excel',fieldnames=fieldnames)
        Class1Writer.writerow({'name':name,'classnumber':classnumber,'score':score})
else:
    with open('class1.csv', "wb") as csv_file:
        headers = {}
        for n in fieldnames:
              headers[n] = n
        Class1Writer = csv.DictWriter(csv_file, delimiter=',',dialect='excel',fieldnames=fieldnames)
        Class1Writer.writerow(headers)
        Class1Writer.writerow({'name':name,'classnumber':classnumber,'score':score})