Python打印到Excel文件

用户名

我正在尝试进行测验,然后将用户的分数存储在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文件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在For循环中将行打印到Excel文件-Python

来自分类Dev

通过Python打印到文件

来自分类Dev

Python从文件读取URL并打印到文件

来自分类Dev

Python-读取Excel文件并将输出打印到另一个文件

来自分类Dev

在python 3中打印到空文件

来自分类Dev

使用Python将格式打印到文件

来自分类Dev

Python,打印到不带“ \ n”功能的文件

来自分类Dev

Python打印到文件不起作用

来自分类Dev

打印到文件Beanstalk Worker Tier(Python)

来自分类Dev

Python:将文件内容打印到终端

来自分类Dev

使用Python将输出打印到文件

来自分类Dev

打印到文件

来自分类Dev

打印到文件

来自分类Dev

使用python将日志文件的内容打印到curses屏幕

来自分类Dev

如何获得Python的ElementTree精美打印到XML文件?

来自分类Dev

在Python中将* s模式打印到.txt文件

来自分类Dev

Python,美丽的汤,如何提取数据并打印到csv文件

来自分类Dev

将循环的输出打印到单个文件 Python

来自分类Dev

将输出打印到Excel

来自分类Dev

用C打印到文件

来自分类Dev

抓取打印到json文件

来自分类Dev

通过wofstream打印到文件

来自分类Dev

从方法内打印到文件

来自分类Dev

不打印到文件Java

来自分类Dev

将PID打印到文件

来自分类Dev

打印到txt文件的方法

来自分类Dev

“打印到文件”的打印选项被禁用

来自分类Dev

在c中打开文件并打印到文件

来自分类Dev

python打印到文件可以在控制台中正确打印,但不能在文件中正确打印