Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 算术测验和对csv的写入_Python_Csv - Fatal编程技术网

Python 算术测验和对csv的写入

Python 算术测验和对csv的写入,python,csv,Python,Csv,我正在创建一个算术测验,但我的代码没有将分数写入excel中的csv文件。你能帮助我吗?我需要修理什么 我有3个空白的csv文件,但其中没有数据。 这是我的密码 import random#this imports random numbers import csv#this imports a csv score=0#this sets the score to be 0 at the start answer=0#this sets the answer to be 0, before it

我正在创建一个算术测验,但我的代码没有将分数写入excel中的csv文件。你能帮助我吗?我需要修理什么

我有3个空白的csv文件,但其中没有数据。 这是我的密码

import random#this imports random numbers
import csv#this imports a csv
score=0#this sets the score to be 0 at the start
answer=0#this sets the answer to be 0, before it is formatted with the different questions
operators=("+","-","*")#this gives three options to select from for a random operation in each question
valid=('gold','platinum','diamond')

#_______________________________________________________________________________________________________________________________

def main():    
    global student_name
    global class_name
    print("ARITHMETIC QUIZ")#this prints the title "ARITHMETIC QUIZ"
    student_name=input("\nWhat is your name? ")#this asks the user to input their name
    #it stores the student's name in the variable 'student_name'

    class_name=input("What class are you in? ")#this asks the user to input their class name
    #it also stores the class name in the variable 'class_name'


    if class_name=="gold":
        quitting()
    elif class_name=="platinum":
        quitting()
    elif class_name=="diamond":
        quitting()
    else:
        print("That is not a valid class name")
        print("Please choose from 'gold', 'platinum' or 'diamond'")
        class_name=input("Please enter your correct class name below: \n")

#_______________________________________________________________________________________________________________________________

def quitting():
    quitting=input("\nDo you want to exit the quiz? (enter 'yes' or 'no') ")
    #this asks the users if they want to exit the program.
    quitting = quitting.lower().replace(' ', '')
    #this converts all the characters to lower case, and it also removes and spaces
    if quitting=='yes':#this checks if they wrote 'yes'
        print("The program will end.")
        #if they did write yes, the above line of text will be printed
        exit()#and then the program will end.
    else:
        quiz()

#_______________________________________________________________________________________________________________________________

def quiz():
    global score
    for i in range(10):
        #this creates a forLoop that will repeat X times, depending on the number inside the brackets
        #in this case, it will repeat the following function 10 times
        num1=random.randint(1,20)#this chooses a random number for the variable num1
        num2=random.randint(1,20)#this chooses a random number for the variable num2
        operator=random.choice(operators)#this chooses a random operation from the options of '+','-' and '*'.

        if operator=="+":
            answer=num1+num2#if the operator was '+', the answer would be the sum of num1 and num2
        elif operator=="-":
            num1,num2 = max(num1,num2), min(num1,num2)#this makes num1 larger than num2 to make subtraction easier
            answer=num1-num2#else if the operator was '-', the answer would be num1 takeaway num2
        elif operator=="*":
            answer=num1*num2#else if the operator was a '*', the answer would be the product of num1 and num2

        try:        
            temp=int(input("\nWhat is " + str(num1) + operator + str(num2) + "? "))#the variable temp changes for each inputted answer
            #this converts the integers 'num1' and 'num2' into strings
            #because integers and strings can't be in the same line of code
            #this asks the user the question
            if temp==answer:#this cheks if the answer is correct
                print("Well Done " + student_name + "! The answer is correct.")
                score=score+1
                #if it is correct, it will print "Well Done! The answer is correct".
                #it will also add 1 to the score
            else:
                print("Incorrect. The answer was " + str(answer))#this converts the variable 'answer' into a string
                score=score#this makes sure that the score remains the same as last time
                #if the answer is not correct, it will print "Incorrect" and output the correct answer
        except ValueError:
            print("Invalid answer. Please use integers only!")
            #this will be printed if the user inputted anything other than an integer, like a string
    print("\nThank you " + student_name + "! You scored " + str(score) + " out of 10 in this quiz.")
    #this converts the variable 'score' into a string
    #and this prints thank you and the student's score out of ten

#_______________________________________________________________________________________________________________________________

def find_user_in_class():
    if class_name == 'gold': #if what the user's input for the class name was 'gold'...
       appendFile = open('goldclassscores.csv', 'a') #it opens the csv file, named 'goldclassscores' 

    elif class_name == 'platinum':#if what the user's input for the class name was 'platinum'...
        appendFile = open('platinumclassscores.csv', 'a')#it opens the csv file, named 'platinumclassscores'

    elif class_name == 'diamond':#if what the user's input for the class name was 'diamond'...
        appendFile = open('diamondclassscores.csv', 'a')#it opens the csv file, named 'diamondclassscores'

    for line in appendFile: #looks through each column of the csv
        user = {}
        (user['student_name']) = line.split
        if student_name == user['student_name']:
            appendFile.close()
    return (user)

    appendFile.close()
    return({})

#_______________________________________________________________________________________________________________________________

def write_to_csv():
    if class_name == 'gold':

        goldcsv = open('goldclassscores.csv', 'a')
        goldcsv.write('\n')
        goldcsv.write(student_name)
        goldcsv.write(str(score))
        goldcsv.close()

        print('Your score has been saved.')

    elif class_name == 'platinum':

        platinumcsv = open('platinumclassscores.csv', 'a')
        platinumcsv.write('\n')
        platinumcsv.write(student_name)
        platinumcsv.write(str(score))
        platinumcsv.close()

        print('Your score has been saved.')

    elif class_name == 'diamond':

        diamondcsv = open('diamondclassscores.csv', 'a')
        diamondcsv.write('\n')
        diamondcsv.write(student_name)
        diamondcsv.write(str(score))
        diamondcsv.close()

        print('Your score has been saved.')

#_______________________________________________________________________________________________________________________________     


if __name__=="__main__": main()

请帮帮我

您没有在任何地方调用write_to_csv()函数。测验结束后试着打电话给它

谢谢,我会试试的,我试过了,谢谢,现在可以用了!!