2016-04-27 8 views
1

Ich versuche, ein Quiz machen, die dann speichert die Punktzahl des Benutzers in einer Excel-Datei, aber wenn ich dies tun, schreibt es alle Noten, die der Benutzer durch die 10 Fragen Quiz - it gibt mir die Punktzahl nach jeder Frage, aber alles, was ich will, ist der Name des Benutzers und ihre Ende Punktzahl in die Datei eingefügt werden. Wie würde ich das in meinem Code unten tun?Python Drucken zu einer Excel-Datei

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

Ich bin mir nicht sicher, ob gerade dabei zwei Werte in eine Excel-Tabelle ist effizienter Weg. Was ist mit dem Versuch, eine einfache Datei zu verwenden? Sie können die Werte weiterhin in einer Excel-Datei speichern, indem Sie eine Bibliothek eines Drittanbieters verwenden: 'xlrd' – direprobs

+0

@direprobs Auch wenn ich eine Textdatei verwende gibt es immer noch die Punktzahl, was die Hauptsache ist, die ich ändern möchte – user6104134

+0

Bitte posten Arbeitscode. Einrückung ist falsch, Variablen wie "operator", "operand1", "operand2" sind niemals definiert. –

Antwort

1

Es ist Druck Punktzahl für jede Frage, da Ihr Zustand in eine CSV-Datei zu schreiben, erfordert nicht die Anzahl der Fragen 10 zu sein. Einrückungen sollte es tun:

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

tun dies bedeutet, dass Sie nur auf eine Ausgabe CSV-Datei schreiben wird, wenn Fragen 10. erreicht haben