[英]Python printing to an excel file
I am trying to make a quiz which then stores the score of the user in an excel file however when I do this it writes all the scores that the user got through out the 10 question quiz - it gives me the score after each question but all I want is the name of the user and their end score to be put into the file.我正在尝试做一个测验,然后将用户的分数存储在一个 excel 文件中,但是当我这样做时,它会写出用户在 10 个问题测验中获得的所有分数 - 它在每个问题之后给我分数,但所有我想要的是将用户名和他们的最终分数放入文件中。 How would I do this in my code below?
我将如何在下面的代码中执行此操作?
import time
import random
question = 0
score = 0
name = input("What is your full name?")
while True:
studentclass = input("What is your class name? Please enter 1, 2 or 3.")
if studentclass.lower() not in ('1', '2', '3'):
print("Not an appropriate choice.")
else:
break
print ("Hello " + name, "welcome to The Arithmetic Quiz. This quiz is for eleven year olds. Use integers to enter the answer!")
time.sleep(2)
operands1 = list(range(2, 12))
operators = ["+","-","x"]
operands2 = list(range(2, 12))
while question < 10:
operand1 = random.choice(operands1)
operand2 = random.choice(operands2)
operator = random.choice(operators)
def inputNumber(message):
while True:
try:
userInput = int(input(message))
except ValueError:
print("Not an integer! Try again.")
continue
else:
return userInput
break
user_answer =int(inputNumber('{} {} {} = '.format(operand1, operator, operand2)))
if operator == '+':
expected_answer = operand1 + operand2
if user_answer==expected_answer:
print('This is correct!')
score = score + 1
question = question + 1
time.sleep(2)
else:
print('This is incorrect!')
question = question + 1
time.sleep(2)
if operator == '-':
expected_answer = operand1 - operand2
if user_answer==expected_answer:
print('This is correct!')
score = score + 1
question = question + 1
time.sleep(2)
else:
print('This is incorrect!')
question = question + 1
time.sleep(2)
if operator == 'x':
expected_answer = operand1 * operand2
if user_answer==expected_answer:
print('This is correct!')
score = score + 1
question = question + 1
time.sleep(2)
else:
print('This is incorrect!')
question = question + 1
time.sleep(2)
if question==10:
endscore = str(score)
print ("Your score is {} out of 10".format(score))
if studentclass == "1":
text_file = open("groupone.csv", "a")
text_file.write (name + "," + (str(score)+ "\n"))
text_file.close()
elif studentclass == "2":
text_file = open("grouptwo.csv", "a")
text_file.write(name + "," + (str(score)+ "\n"))
text_file.close()
else:
text_file = open("groupthree.csv", "a")
text_file.write(name + "," + (str(score)+ "\n"))
text_file.close()
It's printing score for each question since your condition to write into a csv file, does not require the number of questions to be 10 .这是每个问题的打印分数,因为您要写入 csv 文件的条件不需要问题数为 10 。 Indentation should do it:
缩进应该这样做:
if question==10:
endscore = str(score)
print ("Your score is {} out of 10".format(score))
# Below are indented, meaning they are included UNDER the if statement above
if studentclass == "1":
text_file = open("groupone.csv", "a")
text_file.write (name + "," + (str(score)+ "\n"))
text_file.close()
elif studentclass == "2":
text_file = open("grouptwo.csv", "a")
text_file.write(name + "," + (str(score)+ "\n"))
text_file.close()
else:
text_file = open("groupthree.csv", "a")
text_file.write(name + "," + (str(score)+ "\n"))
text_file.close()
Doing this implies that you will be writing to an output csv file only when questions have reached 10.这样做意味着您将仅在问题达到 10 个时写入输出 csv 文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.