Reputation: 1
this is a GPA calculator code in my textbook. I'd like to ask about a few this I don't understand here.
# 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'))
What does current_cumm_gpa, total_credits = cumm_gpa_info
mean below? Does it create a new array? I tried simpler but it doesn't work.
def calculateGPA(sem_grades_info, cumm_gpa_info):
sem_quality_pts = 0
sem_credits = 0
current_cumm_gpa, total_credits = cumm_gpa_info
Upvotes: 0
Views: 1239
Reputation: 6781
Tracing your program we see the following :
>>> 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
So we see that cumm_gpa_info
is a Tuple
. Now this is passed to the function.
>>> ... = 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
So as we can see, the values were unpacked
from the tuple
and assigned to current_cumm_gpa
& total_credits
respectively.
It is similar to doing :
>>> current_cumm_gpa = cumm_gpa_info[0]
>>> total_credits = cumm_gpa_info[1]
Note : sorry if its not exactly how it would look on a console. Can't get to my PC, writing it out on my mobile right now. So have put it approximately how it would look.
Upvotes: 0
Reputation: 4896
From this line:
cumm_gpa_info = (cumm_gpa, total_credits)
We can see that cumm_gpa_info
is a tuple of two values. Then current_cumm_gpa, total_credits = cumm_gpa_info
unpacks the values in the tuple to two variables, the first to current_cumm_gpa
and the second to total_credits
. It's a simpler way of doing:
current_cumm_gpa = cumm_gpa_info[0]
total_credits = cumm_gpa_info[1]
From the docs:
This is called, appropriately enough, sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.
Upvotes: 1