2016-04-28 12 views
0

スコアを.txtファイルに出力するためにこのコードを実行しましたが、エラーはなく、スコアは出力されません。誰もが私がプログラミングにとって非常に新しいことを考える理由を知るのを助けることができますか?ありがとう:)クイズのスコアを.txtファイルに出力する

from random import shuffle 
print ("Welcome to the quiz! ") 
name = input('What is your name?: ') 


with open ("questions.txt") as f: 
    lines = f.readlines() 

shuffle (lines) 
numRight = 0 
wrong = [] 

numQuestions = int(input("How many questions? ")) 

for line in lines [:numQuestions]: 
    question, rightAnswer = line.strip().split("\t") 
    answer = input(question + ' ') 
    rightAnswer = rightAnswer.lower() 
    if answer.lower() == rightAnswer: 
     print ("Right!") 
     numRight +=1 
    else: 
     print ("No, the answer is", rightAnswer) 
     wrong.append(question) 


print ("You got %d right " % (numRight)) 
if (wrong): 
    print ("You got these wrong: ") 
    for q in wrong: 
     print (q) 

user_class = input('What class are you in?: ').lower() 
if user_class=="A": 
    my_file = open("classAScores.txt") 
    my_file.write(name + ' ' +str(numRight)) 
    my_file.close() 

elif user_class =="B": 
    my_file = open("classBScores.txt") 
    my_file.write(name + ' ' + str(numRight)) 
    my_file.close() 

elif user_class=="C": 
    my_file = open("classCScores.txt") 
    my_file.write(name + ' ' +str(numRight)) 
    my_file.close() 
+2

は、なぜあなたはその後、大文字と比較する、あなたの入力下ケースを作っていますか? – StephenTG

+0

@StephenTGハハは決してそのことを考えなかった –

答えて

0

変更コードのこの部分:

user_class = input('What class are you in?: ').lower() 
if user_class=="a": 
    with open("classAScores.txt",'a') as my_file: 
     my_file.write(name + ' ' + str(numRight) + '\n') 
+0

そんなに私はそんなに馬鹿げたエラーをしました! –

0

ない一見から他のすべてについて確認してください、しかし私は確かに一つの論理エラーを見ることができます:

user_class = input('What class are you in?: ').lower() 
if user_class=="A": 

あなたは、大文字のためにチェックし、文字列に.lower()を適用します」 A "となり、決して発生しません。

+0

ハハありがとう –

関連する問題