Question: Hello, for the last line in the code, could anyone show me how to perform it using f' strings, instead of the format() method and join()method. I can use f strings to format the first half of the code,  print(f' {course:<8} {avg_grade:>4.1f}')  In the code, gradebook is the dictionary, course is the key, and grades is the value(grades is in the format of a list) for course, grades in gradebook.items():         grades.sort() # sort the list of grades         sum_grades = sum(grades) # sum all grades for the course         count_grades = len(grades) # determine the number of grades in the list of grades         avg_grade = sum_grades / count_grades # calculate the average for the course         if avg_grade < lowest_avg[1]: # determine the course with the lowest average             lowest_avg = (course, avg_grade)         print("{:<8} {:>4.1f}% {:>25}".format(course, avg_grade, " ".join("{:>4}%".format(g) for g in grades)))

Np Ms Office 365/Excel 2016 I Ntermed
1st Edition
ISBN:9781337508841
Author:Carey
Publisher:Carey
Chapter3: Performing Calculations With Formulas And Functions
Section: Chapter Questions
Problem 3.8CP
icon
Related questions
Question
100%

Question: Hello, for the last line in the code, could anyone show me how to perform it using f' strings, instead of the format() method and join()method. I can use f strings to format the first half of the code, 

print(f' {course:<8} {avg_grade:>4.1f}') 

In the code, gradebook is the dictionary, course is the key, and grades is the value(grades is in the format of a list)

for course, grades in gradebook.items():
        grades.sort() # sort the list of grades
        sum_grades = sum(grades) # sum all grades for the course
        count_grades = len(grades) # determine the number of grades in the list of grades
        avg_grade = sum_grades / count_grades # calculate the average for the course
        if avg_grade < lowest_avg[1]: # determine the course with the lowest average
            lowest_avg = (course, avg_grade)
        print("{:<8} {:>4.1f}% {:>25}".format(course, avg_grade, " ".join("{:>4}%".format(g) for g in grades)))  

Enter course code or press enter to finish: COP1000
Enter course code or press enter to finish: COP2100
Enter course code or press enter to finish: CAP2410
Enter course code or press enter to finish:
Course Avg
Grades
67%
85%
98%
COP1000 76.0% 63% 67%
COP2100 87.6% 70% 87% 90% 95% 96%
CAP2410 82.6% 64% 68% 89% 94% 98%
Term Average: 82.1%
Dropping lowest grade from each course.
Course Avg
COP1000 79.2%
COP2100 92.0%
CAP2410 87.2%
Term Average: 86.2%
Grades
98%
67% 67% 85%
87% 90% 95% 96%
98%
68% 89% 94%
Dropping course COP1000 with lowest average.
Course Avg
COP2100 92.0%
CAP2410 87.2%
Term Average: 89.6%
Grades
96%
94% 98%
87% 90% 95%
68% 89%
Transcribed Image Text:Enter course code or press enter to finish: COP1000 Enter course code or press enter to finish: COP2100 Enter course code or press enter to finish: CAP2410 Enter course code or press enter to finish: Course Avg Grades 67% 85% 98% COP1000 76.0% 63% 67% COP2100 87.6% 70% 87% 90% 95% 96% CAP2410 82.6% 64% 68% 89% 94% 98% Term Average: 82.1% Dropping lowest grade from each course. Course Avg COP1000 79.2% COP2100 92.0% CAP2410 87.2% Term Average: 86.2% Grades 98% 67% 67% 85% 87% 90% 95% 96% 98% 68% 89% 94% Dropping course COP1000 with lowest average. Course Avg COP2100 92.0% CAP2410 87.2% Term Average: 89.6% Grades 96% 94% 98% 87% 90% 95% 68% 89%
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Thank you, I understand how the code is executed. The problem is that I'm only allowed to use either a for loop, or list comprehension to achieve the result. Is there any way to do so without implementing the "join" function or method, as I have not learned that in class yet. 

Here is the exact requirements:

  • use another for loop to print all grades in the grades list for the course. Use a format identifier to allow for printing of a right-justified grade with a width of 4 followed by a percent sign

Is there any way to only use f strings, for loops, and/or list comprehension to achieve the same result. 

Thank you

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

Hello thank you for the solution. Is there also a way to format the list without using the .join method, and just using f' strings, perhaps in a list comprehension? 

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Hash Table
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Np Ms Office 365/Excel 2016 I Ntermed
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage