【发布时间】:2015-01-07 12:33:53
【问题描述】:
我制作了一个 Python 程序,它询问 10 个随机数学问题,然后在最后显示分数,我将我的程序粘贴在下面。我想编辑/改进我的程序,以便它可以存储每个人和每个班级的分数,并且应该存储在 Excel 或记事本文档中。请帮我编辑程序并告诉我哪个函数最适合存储这种数据。请解释并尽可能简单,因为我是python的初学者,所以我不知道很多函数或程序。
import random
def questions():
name=input("What is your name: ")
print("Hello there",name,"! Please answer 10 random maths questions for this test!")
choice = random.choice("+-x")
finish = False
questionnumber = 0
correctquestions = 0
while finish == False:
choice = random.choice("+-x")
if questionnumber < 10 | questionnumber >= 0:
number1 = random.randrange(1,10)
number2 = random.randrange(1,10)
print((number1),(choice),(number2))
answer=int(input("What is the answer?"))
questionnumber = questionnumber + 1
if choice==("+"):
realanswer = number1+number2
if answer==realanswer:
print("That's the correct answer")
correctquestions = correctquestions + 1
else:
print("Wrong answer, the answer was",realanswer,"!")
if choice==("x"):
realanswer = number1*number2
if answer==realanswer:
print("That's the correct answer")
correctquestions = correctquestions + 1
else:
print("Wrong answer, the answer was",realanswer,"!")
elif choice==("-"):
realanswer = number1-number2
if answer==realanswer:
print("That's the correct answer")
correctquestions = correctquestions + 1
else:
print("Wrong answer, the answer was",realanswer,"!")
else:
finish = True
else:
print("Good job",name,"! You have finished the quiz")
print("You scored " + str(correctquestions) + "/10 questions.")
questions()
【问题讨论】:
-
请参阅docs.python.org/2/library/csv.html 以写入可以在例如打开的 csv 文件中。 excel 或其他电子表格软件。
标签: python database storage export-to-excel