AISE1_CLASS/Übung: Clean Code/Student Grade Calculator.py

72 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Bad example
"""
def calc (l) :
t =0
for i in l:
t = t + i
a = t / len (l)
if a >=90:
g = "A"
elif a >=80:
g = "B"
elif a >=70:
g = "C"
elif a >=60:
g = "D"
else :
g = "F"
return g, a
def doeverything (n, s1, s2, s3, s4, s5) :
print ("Processing student :"+ n)
l = [s1, s2, s3, s4, s5]
r = calc (l)
print ("Average :"+ str (r [1]))
print ("Grade :"+ r [0])
if r[1] >= 60:
print ("Status : PASSED")
else:
print ("Status : FAILED")
return r
# main program
x = "John"
doeverything (x,85,90,78,92,88)
print ("---")
y = "Jane"
doeverything (y,55,60,45,50,58)
print ("---")
z = "Bob"
doeverything (z,70,75,80,72,78)
"""
[x] Naming conventions (variables, functions, classes)
[x] Code structure and indentation
[x] Magic numbers and constants
[x] Function length and single responsibility
[ ] DRY principle (Dont Repeat Yourself)
[x] Comments and documentation
[x] Error handling
[x] Whitespace and formatting
[ ] Mutable default arguments
"""
"""
good example
"""
def calculate_avg(points: list[int]) -> float:
return sum(points) / len(points)
def calculate_grade(point_avg: float) -> str:
grade_dict = {
(lambda avg: avg >= 90): "A",
(lambda avg: avg >= 80): "B",
(lambda avg: avg >= 70): "C",
(lambda avg: avg >= 60): "D",
(lambda avg: avg < 60): "F"
}
return grade_dict.get(point_avg)