I wanted to create a function which iterated through a list of grades and transformed “none” into 0, 100 into “perfect”, and added 5 to everything else. Here is how I acheieved it in the end:
I was using ternary operators with for loops and list comprehensions:
grades = [90, 5, 12, 0, “none”, 0, 100]
def grade_curve(grades):
perfect_grades = [“perfect” if m == 100 else m for m in grades]
return [0 if n == “none” else n + 5 if n != “perfect” else n for n in perfect_grades]
print(grade_curve(grades))
The Output:
[95, 10, 17, 5, 0, 5, ‘perfect’]
P.S. I specifically wanted to combined all of these types of codes; just for fun! (I need to go outside).