2017-09-20 20 views
-1

我有一個關於這個代碼的問題:這個Python列表代碼有什麼問題?

menu = "" 
student = [] 
print ("What would you like to do?") 
print ("\t[1] Show full gradebook") 
print ("\t[2] Add Student") 
print ("\t[3] Remove Student") 
print ("\t[4] Modify Student Information") 
print ("\t[5] Display Highest Grade") 
print ("\t[6] Display Lowest Grade") 
print ("\t[7] Quit") 

# loop until the user decides to quit 
while menu != 7: 
    menu = int(input("Enter selection [1-7]")) 
    if menu == 1: 
     print("Name\tGrade") 
     # loop through all students 
     for s in student: 
      print(s[0]+"\t"+str(s[1]))  
    elif menu == 2: 
     # prompt user for student name 
     sname = input("Student Name?") 
     # prompt user for student grade 
     sgrade = int(input("Student Grade?")) 
     # append student information to list 
     student.append([sname, sgrade])  
    elif menu == 3: 
     sname = input("Student to remove?") 
     try: 
      student.remove([sname, sgrade]) 
     except: 
      if sname not in student: 
       print("Not in table.") 
    elif menu == 4: 
     sname = input("Student to modify?") 
     for s in student: 
      print(s[0]+"\t"+str(s[1])) 
     try: 
      student.remove([sname, sgrade]) 
      sname = input("Name: (press Enter to keep original value)") 
      sgrade = int(input("Grade: (press Enter to keep original value)")) 
      student.append([sname, sgrade]) 
     except: 
      if sname not in student: 
       print("Not in table.") 
    elif menu == 5: 
     try: 
      print(sname + " had the highest score in the class: " + str(sgrade)) 
     except: 
      pass 
    elif menu == 6: 
     try: 
      print(sname + " had the lowest score in the class: " + str(sgrade)) 
     except: 
      pass 
    elif menu >= 8: 
     print("Invalid selection.") 

print ("Terminating program... Goodbye!") 

每次我嘗試選擇3至6與多個學生,它會做它爲學生在列表的底部。另外,當我修改學生信息時,我想知道如何保留某個學生的原名或成績。

+1

「做」 - 做什麼? – DyZ

+0

如果您將每個操作移至不同的功能,您將看到問題。並且將while循環移動到主函數。您正在重複使用前一次迭代中的'sgrade'變量 – balki

回答

1

在每次操作之前,代碼並未從列表中檢索學生的詳細信息。而是使用前一個值sgrade,並且sgrade將始終爲列表中最後一名學生使用的值。因此選項3-6只適用於最後一名學生。

您可以在搜索列表時僅使用學生姓名來修復它。例如,要刪除學生(選項3),你可以使用列表理解:

student = [s for s in student if s[0] != sname] 

在你的代碼:

elif menu == 3: 
    sname = input("Student to remove?") 
    len_orig = len(student) 
    student = [s for s in student if s[0] != sname] 
    if len_orig == len(student): 
     # length unchanged therefore student not in list 
     print("Not in table.") 

方案4以上的變化。要找到最高年級的學生,你可以使用max()功能:

highest = max(student, key=lambda x: x[1]) 

同樣最低可以min()發現:

lowest = min(student, key=lambda x: x[1]) 

在數據結構方面,一本字典是一個比列表更好的選擇。使用學生姓名作爲密鑰,並將等級作爲值。然後,如添加,刪除或修改學生作業是微不足道的:

students = {} # initialise 

# add a student 
sname = input("Student Name?") 
sgrade = int(input("Student Grade?")) 
students[sname] = sgrade 

# remove student 
sname = input("Student to remove?") 
if sname in students: 
    del students[sname] 
else: 
    print("Not in table.") 

+0

我不應該使用字典。 –

+0

任何選項4-6?畢竟他們是不同的。 –

+0

好吧,這是不幸的。然後你需要搜索學生名單來執行操作。我已經展示了一個使用列表理解從列表中移除項目的例子。 – mhawke