【问题标题】:Python printing to an excel filePython打印到excel文件
【发布时间】:2016-08-21 10:45:22
【问题描述】:

我正在尝试做一个测验,然后将用户的分数存储在一个 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()

【问题讨论】:

  • 我不确定将两个值放入 Excel 工作表是否有效。尝试使用一个简单的文件怎么样?您仍然可以使用第三方库将值存储在 Excel 文件中:xlrd
  • @direprobs 即使我使用文本文件,它仍然给出十倍的分数,这是我要更改的主要内容
  • 请发布工作代码。缩进错误,operatoroperand1operand2等变量从未定义。
  • @marktolonen 我没有包含我的整个代码 - 这是之前定义的。我只添加了我的代码的相关部分。
  • 如果缩进错误,我们怎么知道你的问题是什么?这在 Python 中很重要。无法使用提供的代码重现您的问题。最有可能的是,您的文件编写代码在 while 循环内询问问题,而在询问完所有问题后何时应该在循环外,但无法确定。

标签: python excel file store


【解决方案1】:

它是打印每个问题的分数,因为您的条件写入 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 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-08
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 2014-08-16
    • 2018-08-16
    相关资源
    最近更新 更多