當前位置:成語大全網 - 新華字典 - python怎麽將學生的成績保存成字典 並返回三門課的平均值

python怎麽將學生的成績保存成字典 並返回三門課的平均值

global students

students={

"Zhangsan":(89,88,80),

"Bill":(90,90,86),

"Tom":(80,98,85),

"Jack":(79,90,89)}

def average(*shu):

(Tab縮進)AVE=sum(shu)/len(shu)

(Tab縮進)return AVE

def average_courses(name):

(Tab縮進)for k,v in students.items():

(Tab縮進)(Tab縮進)if k==name:

(Tab縮進)(Tab縮進)(Tab縮進)return [name, v, round(average(*v), 1)]

if __name__ == "__main__":

(Tab縮進)#print(average_courses("Tom"))

(Tab縮進)#print(average_courses("Tom")[2])

(Tab縮進)for k in students.keys():

(Tab縮進)(Tab縮進)print([k,average_courses(k)[2]])

'''python運行效果

['Zhangsan', 85.7]

['Bill', 88.7]

['Tom'