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)))
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%](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fddbbafc5-f5c4-4a06-b5e5-c3c23263691b%2Feee97cfc-5841-4ccf-9b98-45ac8b17c2e2%2Fahc6wc6_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
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
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?