2016-03-09 26 views
0

回到關於我的項目的另一個問題。所以我試圖在我的文本文件中按降序排列我的分數。但是,它會輸出語法錯誤。 當用戶輸入class_name時,上面創建的文件名是這樣,那麼什麼等於文件名。但是,它輸出一個錯誤。按字母順序排列分數和名稱,平均值,從最高到最低

這是我的一些代碼:

filename = class_name + ".txt"      
with open(filename, 'a+') as w:      
    w.write(str(name) + " : " + str(score) + '\n') 
    print("Your results are being updated ...") 
    time.sleep(2) 
    print("Your score has been updated") 
    print("") 
    w.close() 
if get_bool_input("Do you wish to view the previous results from your class: "): #The 'get_bool_input' will determine if they input yes or no and will either open the file or not. 
    selection= input("Do you wish to view the results in Alphabetical order(A), scores highest to lowest(B) or average score highest to lowest?(C)") 
    if selection == 'A': 
     with open(filename, 'r') as r: 
      for name in sorted(r): 
       print(name, end='') 
    if selection == 'B': 
     with open(filename, 'r') as r: 
      file_sorted = sorted((ast.literal_eval(x) for x in r),key=lambda z:(int(z[1]),z[0]),reverse=True)   
      r.close() 
    if selection not in ['A','B','C']: 
     print ("Error, type in A, B or C.") 

如何使它循環回「選擇=」的問題嗎?如果A,B或C未被選中。

if get_bool_input("Do you wish to view the previous results from your class: "): #The 'get_bool_input' will determine if they input yes or no and will either open the file or not. 
    selection= input("Do you wish to view the results in Alphabetical order(A), scores highest to lowest(B) or average score highest to lowest?(C)") 
    if selection == 'A': 
     print (alphabetically(data)) 
    if selection == 'B': 
     print (by_score(data)) 
    if selection == 'C': 
     print (by_average(data)) 

    if selection not in ['A','B','C']: 
     print ("Error, type in A, B or C.") 

else: 
    input ("Press any key to exit") 

編輯

像這樣的事情?

while True: 
    if selection == 'A': 
     print (alphabetically(data)) 
    elif selection == 'B': 
     print (by_score(data)) 
    elif selection == 'C': 
     print (by_average(data)) 
    return True 
    else: selection not in ['A','B','C']: 
     print ("Error, type in A, B or C.") 
+0

不回答的主要問題,但你可能想'如果選擇不['A','B','C']' – pkacprzak

+0

是的,幾乎同樣的事情不是嗎?並且,如果他們不輸入A,B或C,我將如何使它回到他們輸入A,B或C的位置。 – Draguno

回答

1

第一個開放的,嘗試:

with open(filename, 'a+') as write_file: 
.... 

那麼第二個開做:

with open(filename, 'r+') as read_file: 
... 

也許它會工作

編輯

現在首先對您的文件的解析和分類方法,我想出了這個:

from collections import defaultdict 
from collections import OrderedDict 

filename = 'file.txt' 


def alphabetically(some_data): 
    return OrderedDict(
     (k, some_data[k]['scores']) 
     for k in sorted(some_data) 
    ) 


def by_score(some_data, descending=True): 
    return OrderedDict(
     (k, sum(some_data[k]['scores'])) 
     for k in sorted(some_data, 
         key=lambda k: sum(some_data[k]['scores']), 
         reverse=descending) 
    ) 


def by_average(some_data, descending=True): 
    def average(scores): 
     return float(sum(scores))/len(scores) 
    return OrderedDict(
     (k, average(some_data[k]['scores'])) 
     for k in sorted(some_data, 
         key=lambda k: average(some_data[k]['scores']), 
         reverse=descending) 
    ) 


data = defaultdict(dict) 
with open(filename, 'r+') as f: 
    for line in f.read().splitlines(): 
     name, score = line.split(' : ') 
     scores = data[name].get('scores', []) 
     scores.append(int(score)) 
     data[name]['scores'] = scores 

print alphabetically(data) 
print by_score(data) 
print by_average(data) 

輸出:

OrderedDict([('ADAM', [2]), ('Da', [3, 0, 1]), ('Dadsid', [4]), ('Davd', [3, 4]), ('Dliid', [9]), ('Dloed', [1]), ('Dsid', [3]), ('lukedd', [8]), ('mathe', [4, 12])]) 
OrderedDict([('mathe', 16), ('Dliid', 9), ('lukedd', 8), ('Davd', 7), ('Da', 4), ('Dadsid', 4), ('Dsid', 3), ('ADAM', 2), ('Dloed', 1)]) 
OrderedDict([('Dliid', 9.0), ('lukedd', 8.0), ('mathe', 8.0), ('Dadsid', 4.0), ('Davd', 3.5), ('Dsid', 3.0), ('ADAM', 2.0), ('Da', 1.3333333333333333), ('Dloed', 1.0)]) 
+0

好吧,它好像修復了你讀的問題,你準確地完成了我寫的內容以上?這在您的代碼中尚未更新。現在,您似乎在處理讀取的數據時遇到了問題,這本身就是另一個問題。您能否告訴我們該文件包含哪些文件包含的內容? –

+0

重要信息:爲了能夠計算總和或平均值,我爲同一用戶添加了更多行,並附加分數 –

+0

非常感謝。添加了一些東西給我的代碼,它的工作!如果沒有選擇A,B或C,是否有任何方法可以讓它循環回選擇?更新我的代碼。 – Draguno

相關問題