我做了一个python测验,我需要让它将所有分数记录到一个文本文件中,我很困惑 我怎么能得到它来得分和名称到文本文件?
import random
correct = 0
name = input("Please enter your name")
for count in range(10):
num1 = random.randint(1, 100)
num2 = random.randint(1, 100)
symbol = random.choice(["+", "-", "*"])
print("Please solve:\n", num1, symbol, num2)
user = int(input(""))
if symbol == "+":
answer = num1 + num2
elif symbol == "-":
answer = num1 - num2
elif symbol == "*":
answer = num1 * num2
if user == answer:
print("Correct!")
correct = correct + 1
else:
print("incorrect")
print(name, ", You got", Correct, "Out of 10")
提前致谢。
答案 0 :(得分:1)
在Py3中非常简单
将您的print
声明更改为
print(name, ", You got", Correct, "Out of 10", file= "ClassScore..txt")
但是如果你使用py2
然后
with open("ClassScore.tx.txt","w") as f:
f.write("{}, You got {} Out of 10".format(name,Correct))
答案 1 :(得分:0)
with open("totalscore.txt","a+") as f:
f.write(name)
f.write(correct)
将此行添加到您的代码的末尾。但是您可以通过google上的一些搜索轻松找到它。