def AddStudent():
#Variable to control the loop
again = 'y'
#While loop that gets user input to be added to dictionary
while again.lower() == 'y':
student_key = input("What's the name of the student? ")
#Ask user for number of grades
n = int(input('How many grades do you wish to input? '))
#Empty list where grades will be saved
grades = []
#for loop to add as many grades as user wishes
for i in range(0 ,n):
grade = int(input(f'Grade {i 1}: '))
grades.append(grade)
#Call StudentGradeBook and send values as parameters
StudentGradeBook(student_key, grades)
again = input('Do you wish to add another student? (y) ')
def StudentGradeBook(name, grade_value):
#Dictionary of the grades
grade_book = {'Tom':[90,85,82], 'Bob':[92,79,85]}
#Add the key and value to the dict
grade_book[name] = grade_value
print(grade_book)
當我在字典中添加多個姓名和成績串列時,它只會替換第三個,而不是添加第四個、第五個等。
這是輸出:
What's the name of the student? Bill
How many grades do you wish to input? 3
Grade 1: 88
Grade 2: 88
Grade 3: 88
{'Tom': [90, 85, 82], 'Bob': [92, 79, 85], 'Bill': [88, 88, 88]}
Do you wish to add another student? (y) y
What's the name of the student? Thomas
How many grades do you wish to input? 3
Grade 1: 87
Grade 2: 88
Grade 3: 88
{'Tom': [90, 85, 82], 'Bob': [92, 79, 85], 'Thomas': [87, 88, 88]}
Do you wish to add another student? (y) n
uj5u.com熱心網友回復:
我建議您將所有輸入保存在一個串列中,然后將串列傳遞給StudentGradeBook():
def AddStudent():
# Variable to control the loop
again = 'y'
# Keep track of your inputs
inputs_list = []
# While loop that gets user input to be added to dictionary
while again.lower() == 'y':
student_key = input("What's the name of the student? ")
# Ask user for number of grades
n = int(input('How many grades do you wish to input? '))
# Empty list where grades will be saved
grades = []
# for loop to add as many grades as user wishes
for i in range(0, n):
grade = int(input(f'Grade {i 1}: '))
grades.append(grade)
# Save the inputs before calling StudentGradeBook
inputs_list.append([student_key, grades])
again = input('Do you wish to add another student? (y) ')
# Call StudentGradeBook and pass the inputs as a list
StudentGradeBook(inputs_list)
def StudentGradeBook(grades):
grade_book = {'Tom': [90, 85, 82], 'Bob': [92, 79, 85]}
grade_book.update(grades)
print(grade_book)
uj5u.com熱心網友回復:
您的StudentGradebook功能始終以:
grade_book = {'Tom':[90,85,82], 'Bob':[92,79,85]}
并且由于它既不回傳修改后的結果dict也不將其存盤在其他地方,因此它總是從頭開始重新啟動。如果您想在通話中保留一個學生成績簿,我建議將其設為一個班級,并重用該班級的一個實體來添加新的學生資訊,例如像這樣定義它:
class StudentGradeBook:
def __init__(self):
# Initial dictionary on creation
self.grade_book = {'Tom':[90,85,82], 'Bob':[92,79,85]}
def add_grades(self, name, grades):
# Update with additional data
self.grade_book[name] = list(grades) # Shallow copy to avoid being tied to caller list
# Optionally, if new grades for an existing student should be allowed,
# replace the line above with:
self.grade_book.setdefault(name, []).extend(grades)
# which will concatenate on new grades for the name rather than replacing all grades
并像這樣使用它:
def AddStudent(gradebook=None): # Allow passing in an existing gradebook
if gradebook is None:
gradebook = StudentGradebook() # Make a new one if it one wasn't provided
#Variable to control the loop
again = 'y'
#While loop that gets user input to be added to dictionary
while again.lower() == 'y':
student_key = input("What's the name of the student? ")
#Ask user for number of grades
n = int(input('How many grades do you wish to input? '))
#Empty list where grades will be saved
grades = []
#for loop to add as many grades as user wishes
for i in range(0 ,n):
grade = int(input(f'Grade {i 1}: '))
grades.append(grade)
#Call StudentGradeBook and send values as parameters
gradebook.add_grades(student_key, grades)
print(gradebook.grade_book) # Print the state so far
again = input('Do you wish to add another student? (y) ')
return gradebook # So caller can use it if they didn't already provide one
uj5u.com熱心網友回復:
每次你打電話時,StudentGradeBook你都是grade_book從頭開始重新定義為
grade_book = {'Tom':[90,85,82], 'Bob':[92,79,85]}
只需將其移出兩個函式體即可。
def AddStudent():
...
#Dictionary of the grades
grade_book = {'Tom':[90,85,82], 'Bob':[92,79,85]}
def StudentGradeBook(name, grade_value):
#Add the key and value to the dict
grade_book[name] = grade_value
print(grade_book)
uj5u.com熱心網友回復:
您還可以將grade_book函式中的關鍵字引數作為默認值傳遞:
def StudentGradeBook(name, grade_value, grade_book={'Tom': [90, 85, 82], 'Bob': [92, 79, 85]}):
完整代碼:
def AddStudent():
again = 'y'
while again.lower() == 'y':
StudentGradeBook(input("What's the name of the student? "), [int(input(f'Grade {i 1}: '))
for i in range(int(input('How many grades do you wish to input? ')))])
again = input('Do you wish to add another student? (y) ')
def StudentGradeBook(name, grade_value, grade_book={'Tom': [90, 85, 82], 'Bob': [92, 79, 85]}):
grade_book[name] = grade_value
print(grade_book)
輸出:
What's the name of the student? Bill
How many grades do you wish to input? 3
Grade 1: 88
Grade 2: 88
Grade 3: 88
{'Tom': [90, 85, 82], 'Bob': [92, 79, 85], 'Bill': [88, 88, 88]}
Do you wish to add another student? (y) y
What's the name of the student? Thomas
How many grades do you wish to input? 3
Grade 1: 87
Grade 2: 88
Grade 3: 88
{'Tom': [90, 85, 82], 'Bob': [92, 79, 85], 'Bill': [88, 88, 88], 'Thomas': [87, 88, 88]}
Do you wish to add another student? (y) n
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/519551.html
標籤:Python功能循环字典
