2016-04-28 85 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

謝謝soooo多這樣我做了這樣一個愚蠢的錯誤! –

0

不知道有關一切從第一眼,不過,我可以看到一個邏輯上的錯誤肯定的:

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

您申請.lower()字符串,然後檢查大寫「一個「,這將永遠不會發生。

+0

哈哈耶謝謝 –