gpt4 book ai didi

Python 打印到 Excel 文件

转载 作者:太空宇宙 更新时间:2023-11-03 16:40:05 25 4
gpt4 key购买 nike

我正在尝试做一个测验,然后将用户的分数存储在 Excel 文件中,但是当我这样做时,它会写入用户在 10 个问题测验中获得的所有分数 - 它会在每个问题后给我分数问题,但我想要的只是将用户的姓名及其最终得分放入文件中。我将如何在下面的代码中执行此操作?

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()

最佳答案

它是打印每个问题的分数,因为你的条件是写入csv文件,不要求问题数量为10。缩进应该做到这一点:

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()

执行此操作意味着仅当问题达到 10 时您才会写入输出 csv 文件。

关于Python 打印到 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36884689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com