2017-05-07 73 views
0

我試圖創建一個列表,其中列出5個學生信息,然後傳遞給學生類。看起來我已經成功完成了這個任務,但現在我無法弄清楚如何訪問和修改列表中的任何以前添加的值。Python將列表傳遞給類,並試圖引用索引值

class Student: 

    def __init__(self, student_name, student_id, student_gpa, student_grade, student_time): 
     self.student_name = student_name 
     self.student_id = student_id 
     self.student_gpa = student_gpa 
     self.student_grade = student_grade 
     self.student_time = student_time 

student_list = [] 
for i in range(5): 
    student_name = input("Please enter student's name: ") 
    student_id = input("Please enter student's ID number: ") 
    student_gpa = input("Please enter student's GPA: ") 
    student_grade = input("Please enter student's expected grade: ") 
    student_time = input("Please enter if student is a part or full time student: ") 
    student_list.append(Student(student_name, student_id, student_gpa, student_grade, student_time)) 

我怎麼可能會添加引用一個學生的名字和改變從什麼最初進入等級的選項?

+2

要通過名稱訪問學生,最好使用字典,而不是列表。 – JohanL

回答

1

這裏有一個Student對象列表,列表非常小。因此,您可以遍歷列表並找到學生。 在這裏,學生ID是識別特定學生的唯一方式。

def change_gpa(id): 
    for student in student_list: 
     if student.student_id == id: 
      # Take input from the user of the changes to be made 
      # Example gpa 
      student.student_gpa = input('Enter new gpa') 
      return 
change_gpa(input('Enter the student id whose credentials have to be changed')) 
+0

一旦你找到學生,你應該跳出循環 – Barmar

+0

感謝您指出。更改完成@Barmar – bigbounty

1

您ahve含學生類的實例的列表,你可以簡單地遍歷它,例如:

for student in student_list: 
    if student.student_id == something: 
     student.student_grade = whatever #it changes the value 

,或者,如果你知道你想要的,你可以做列表修改究竟哪些學生:

student_list[0].student_grade = whatever #assuming you want to modify the first student in the list 
1

的Python 3:

class Student: 
    def __init__(self, student_name, student_id, student_gpa, student_grade, student_time): 
     self.student_name = student_name 
     self.student_id = student_id 
     self.student_gpa = student_gpa 
     self.student_grade = student_grade 
     self.student_time = student_time   

student_list = [] 
for i in range(3): 
    student_name = input("Please enter student's name: ") 
    student_id = input("Please enter student's ID number: ") 
    student_gpa = 3.73 
    student_grade = "B-" 
    student_time = 2017 
    ''' 
    student_gpa = input("Please enter student's GPA: ") 
    student_grade = input("Please enter student's expected grade: ") 
    student_time = input("Please enter if student is a part or full time student: ") 
    ''' 
    student_list.append(Student(student_name, student_id, student_gpa, student_grade, student_time)) 

print("Origianl Information") 
for student in student_list: 
    print(student.student_id,student.student_name,student.student_gpa) 

search_student = input("Enter a student name to change GPA: ") 
for student in student_list: 
    if student.student_name == search_student: 
     student_gpa = input("Enter new GPA of "+search_student+": ") 
     student.student_gpa = student_gpa 

print("Updated Information") 
for student in student_list: 
    print(student.student_id,student.student_name,student.student_gpa) 

輸出:

Please enter student's name: Shovon 
Please enter student's ID number: 2389 
Please enter student's name: Aslam 
Please enter student's ID number: 2383 
Please enter student's name: Nafis 
Please enter student's ID number: 56 
Origianl Information 
2389 Shovon 3.73 
2383 Aslam 3.73 
56 Nafis 3.73 
Enter a student name to change GPA: Shovon 
Enter new GPA of Shovon: 3.96 
Updated Information 
2389 Shovon 3.96 
2383 Aslam 3.73 
56 Nafis 3.73 

N.B:我評論過一些線減少的巨大投入。取消這些評論。