0
私は算術クイズを作成していますが、私のコードはExcelのCSVファイルにスコアを書き込んでいません。手伝って頂けますか?修正する必要があるのは何ですか?算術クイズとCSVへの書き込み
空のcsvファイルが3つありますが、データはありません。 私のコードは...
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()
私を手伝ってください!
私はそれを試してみます –
私はそれを試してみました、ありがとう、それは今働きます! –