livepython.net

149. Python Program to Clean Up a Messy Report Card

HardClean code

Overview

The whole chapter in one program: it has a bug that stops it, and it breaks almost every rule of PEP 8.

Problem Statement

The starter code reads a student's name and their marks, then prints a report with the average (2 decimals) and a grade: 80 or more is A, 60 or more is B, 40 or more is C, anything lower is F. For example, for Sunny and 78 92 85:

Report for Sunny
Average: 85.00
Grade: A

First, fix the bug that stops it. Then clean it up:

  • clear snake_case names for every variable and function; function names start with a verb,
  • no variable named after a built-in function, like sum,
  • the grade limits 80, 60 and 40 as UPPER_CASE constants at the top,
  • one statement per line: no semicolons, and every block on its own lines with 4 spaces,
  • one space around operators and after commas, and two blank lines around each function,
  • no dead code: no commented-out lines, nothing after a return,
  • a docstring for each function.

Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

The tests check only the output. Once the bug is fixed, the messy code passes them too, so the clean-up is up to you: go through the list above, one rule at a time.

Input

One value per line, in this order: the name, then the marks separated by spaces. Example:

Sunny
78 92 85

Output

The output for that example:

Report for Sunny
Average: 85.00
Grade: A

The tests try 5 different inputs.

Learn this firstChapter 16 · Clean Code in Python: Habits Every Beginner Needs
solution.py
def X(m):
    sum=0
    for i in m:sum+=i
    return sum/len(m)
    print("average done")
def g(a):
    if a>=80:return "A"
    elif a>=60:return "B"
    elif a>=40:return "C"
    return "F"
n=input();M=[]
for p in input().split():M.append(int(p))
#print(M)
A=X(M)
print("Report for",N)
print(f"Average: {A:.2f}")
print("Grade:",g(A))

EXPECTED OUTPUT

1Report for Sunny
2Average: 85.00
3Grade: A

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.