这是我教科书中的GPA计算器代码。我想问一些我不明白的事情。
# Semester GPA Calculation
def convertGrade(grade):
if grade == 'A+':
return 4
if grade == 'A':
return 3.7
if grade == 'A-':
return 3.3
if grade == 'B+':
return 3.0
if grade == 'B':
return 2.7
if grade == 'B-':
return 2.3
if grade == 'C+':
return 2.0
if grade == 'C':
return 1.7
if grade == 'C-':
return 1.3
if grade == 'D+':
return 1.0
if grade == 'D':
return 0.7
if grade == 'D-':
return 0.3
else:
return 0
def getGrades():
semester_info = []
more_grades = True
empty_str = ''
while more_grades:
course_grade = input('Enter grade (hit Enter if done): ')
while course_grade not in ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'D-', 'E+', 'E', 'E-', 'F', empty_str]:
course_grade = input('Enter letter grade you received: ')
if course_grade == empty_str:
more_grades = False
else:
num_credits = int(input('Enter number of credits: '))
semester_info.append([num_credits, course_grade])
return semester_info
def calculateGPA(sem_grades_info, cumm_gpa_info):
sem_quality_pts = 0
sem_credits = 0
current_cumm_gpa, total_credits = cumm_gpa_info
for k in range(len(sem_grades_info)):
num_credits, letter_grade = sem_grades_info[k]
sem_quality_pts = sem_quality_pts + \
num_credits * convertGrade(letter_grade)
sem_credits = sem_credits + num_credits
sem_gpa = sem_quality_pts / sem_credits
new_cumm_gpa = (current_cumm_gpa * total_credits + sem_gpa * \
sem_credits) / (total_credits + sem_credits)
return (sem_gpa, new_cumm_gpa)
# ---- main
# program greeting
print('This program calculates new semester and cumulative GPAs\n')
# get current GPA info
total_credits = int(input('Enter total number of earned credits: '))
cumm_gpa = float(input('Enter your current cummulative GPA: '))
cumm_gpa_info = (cumm_gpa, total_credits)
# get current semester grade info
print()
semester_grades = getGrades()
# calculate semester gpa and new cumulative gpa
semester_gpa, cumm_gpa = calculateGPA(semester_grades, cumm_gpa_info)
#display semester gpa and new cummulative gpa
print('\nYour semester GPA is', format(semester_gpa, '.2f'))
print('Your new cummulative GPA is', format(cumm_gpa, '.2f'))
current_cumm_gpa, total_credits = cumm_gpa_info
下面的含义是什么?它会创建一个新阵列吗?我试过更简单,但它不起作用。
def calculateGPA(sem_grades_info, cumm_gpa_info):
sem_quality_pts = 0
sem_credits = 0
current_cumm_gpa, total_credits = cumm_gpa_info
答案 0 :(得分:1)
从这一行:
cumm_gpa_info = (cumm_gpa, total_credits)
我们可以看到cumm_gpa_info
是两个值的a tuple。然后current_cumm_gpa, total_credits = cumm_gpa_info
将元组中的值解包为两个变量,第一个变为current_cumm_gpa
,第二变为total_credits
。这是一种更简单的做法:
current_cumm_gpa = cumm_gpa_info[0]
total_credits = cumm_gpa_info[1]
来自文档:
这足够恰当地称为序列解包并且适用于 右侧的任何序列。序列拆包需要这样做 在等号的左侧有多少变量 序列中有元素。请注意,多个赋值是 实际上只是元组打包和序列解包的组合。
答案 1 :(得分:0)
跟踪您的计划,我们会看到以下内容:
>>> total_credits = int(input('Enter total number of earned credits: '))
#3.2
>>> cumm_gpa = float(input('Enter your current cummulative GPA: '))
#4.6
>>> cumm_gpa_info = (cumm_gpa, total_credits)
>>> cumm_gpa_info
(3.2 , 4.6)
>>> type(cumm_gpa_info)
Tuple
因此,我们发现cumm_gpa_info
是Tuple
。现在将其传递给函数。
>>> ... = calculateGPA(semester_grades, cumm_gpa_info)
#inside function
>>> current_cumm_gpa, total_credits = cumm_gpa_info
#current_cumm_gpa, total_credits = (3.2 , 4.6)
>>> current_cumm_gpa
3.2
>>> total_credits
4.6
我们可以看到,unpacked
中的值为tuple
,并分配给current_cumm_gpa
&分别为total_credits
。
类似于:
>>> current_cumm_gpa = cumm_gpa_info[0]
>>> total_credits = cumm_gpa_info[1]
注意:抱歉,如果它不完全是在控制台上看起来的样子。无法访问我的电脑,现在就把它写在我的手机上。所以大概看看它的样子。