2017-12-03 298 views
-1

大家好,所以我創建一個學生成績的菜單,我想進入一個學生的名字,並使用鹹菜2個測驗成績。一旦輸入這些成績,我想創建一個報告,並且在我嘗試爲我的菜單選項2搜索單個學生並向菜單選項三顯示所有學生時,但每次嘗試打印時都會收到錯誤代碼它。錯誤代碼試圖加載個別的學生級菜單,所有學生

import pickle 

def menu(): 
    selection = input("0\tExit" 
        "\n1\tEnter Student Name/Grades" 
        "\n2\tIndividual Report" 
        "\n3\tReports" 
        "\nEnter Menu Number: ") 
    if selection == "0": 
    systemExit() 
    if selection == "1": 
    studentData() 
    if selection == "2": 
    singleReport() 
    if selection == "3": 
    studentReports() 

def systemExit(): 
    exit() 

def studentData(): 
    name = input("Enter Student Name: ") 
    quiz1 = input("Enter Quiz 1: ") 
    quiz2 = input("Enter Quiz 2: ") 

with open("pStudent_Quiz_Grades.p", "ab") as pFile: 
    pickle.dump((name, (quiz1, quiz2)), pFile) 
clearScreen() 
return() 

def clearScreen(): 
    print("\n" * 5) 
    return() 

def singleReport(): 
    pFile = open("pStudent_Quiz_Grades.p", "rb") 
    grades_dict = pickle.load(pFile) 

    search = input("Enter a Name to Search: ") 

    for name in grades_dict: 
    if name.upper() == search.upper(): 
     print(name+": "+str("pStudent_Quiz_Grades.p"[name])) 

def studentReports(): 
    pFile = pickle.load(open("pStudent_Quiz_Grades.p", "rb")) 
    print(pFile) 

while True: 
    menu() 
+0

什麼錯誤和在哪一行? – aBiologist

+0

你必須添加該錯誤。 – hasanghaforian

+0

誤差基本上是「高清singleReport()」和「高清studentReports()」,但該錯誤代碼,我得到的是針對「高清singleReport():」它說「元組對象有沒有屬性」,並在「高清studentReports():」我試圖讓所有學生的成績展現出來,但只有一個顯示出來@aBiologist – Bray98

回答

0

我立足我的答案只有我已經被賦予了錯誤,讀作:

Traceback (most recent call last): File "D:/PyCharm Projects/Fundamentals of Programming/StuGrades HW.py", line 57, in <module> menu() File "D:/PyCharm Projects/Fundamentals of Programming/StuGrades HW.py", line 21, in menu singleReport() File "D:/PyCharm Projects/Fundamentals of Programming/StuGrades HW.py", line 49, in singleReport if name.upper() == search.upper(): AttributeError: 'tuple' object has no attribute 'upper' 

的錯誤,指出的名字是一個元組,其不能有方法上,只有串允許此方法。所以我建議使用打印做一個簡單的調試,你可以這樣做:

for name in grades_dict: 
print "name", name "and type is", type(name) 
if name.upper() == search.upper(): 
    print(name+": "+str("pStudent_Quiz_Grades.p"[name])) 

與此打印語句,您可以檢查該名稱是什麼樣子,以及數據名稱的類型是很明顯應該是個元組。另外,如果我們假設搜索是一個字符串變量,那麼您不能將字符串與元組進行比較。所以我建議在你做完調試之後,就是將名稱轉換爲字符串,這樣你可以讓程序比較搜索是否是字符串。

+0

好的,我會嘗試!謝謝@a生物學家 – Bray98