【发布时间】:2015-10-23 22:55:17
【问题描述】:
我正在为数学测验编写代码,在这个测验中,我需要能够将数据存储到文本文件中。数据是测验结束时的分数。我需要它输出到这样的文本文件中......
> Tom,4,5,7
> Evie,6,10,8
目前我只有这个
import random
Class=input("What class are you in?")
name=input("What is your name?")
print("Welcome to the maths quiz",name)
print("Try to answer all the questions with the correct number.")
score=0
questionnumber=0
while questionnumber<10:
Number1=random.randrange(1,10)
Number2=random.randrange(1,10)
Operation=random.randrange(1,4)
if(Operation==1):
symbol= " + "
correctAnswer = Number1 + Number2
elif(Operation==2):
symbol= " - "
correctAnswer = Number1 - Number2
else:
symbol= " * "
correctAnswer = Number1 * Number2
question=(str(Number1)+str(symbol)+str(Number2)+"=?")
useranswer=(float(input(question)))
if useranswer==correctAnswer:
score=score+1
print("Well Done, Correct. Your score is now ",score,"/10")
questionnumber=questionnumber+1
else:
print("Incorrect, sorry. Score:",score)
questionnumber=questionnumber+1
else:
print(name," you finished with a score of ",score,"/10")
if(Class==1):
fi=open("Class1.txt","a")
fi.writelines("\n"+name+":"+str(score))
fi.close
elif (Class== 2):
fi=open("Class2.txt","a")
fi.writelines("\n"+name+":"+str(score))
fi.close
elif (Class== 3):
fi=open("Class3.txt","a")
fi.writelines("\n"+name+":"+str(score))
fi.close
输出看起来像这样......
Tom:4
Tom:5
Tom:7
Evie:6
Evie:10
Evie:8
任何帮助将不胜感激。
【问题讨论】:
-
什么没有按预期工作?
-
您在哪里/如何定义名称和分数?我建议在输入之前对您的数据进行标记
-
它输出很好,但是它需要像我上面显示的那样输出 csv 样式,我只是不太知道该怎么做
-
不过,R Nar 的意思是,您包含的代码并未显示您从哪里获得
name和score。 -
请做你自己的GCSE课程。如果你被卡住了,和你的老师谈谈。如果你要在这里问,至少先学How to Ask。
标签: python arrays list sorting text-files