Jan-30-2022, 01:54 PM
I think I would write 3 functions: max_mark(marks, students), total_marks(marks) and get_input(). max_marks does what your current function does, except it does not loop, it does not ask for input, and it returns a tuple (student, score). Since choice 6 is completely different than choices 1-5 I would write that as a separate function instead of messing up max_mark() trying to make it do two different things.
get_input() displays a prompt, gets user input, VERIFIES USER INPUT, and returns user input. I would write it to return something more friendly than 1, 2, 3... Perhaps "Math", "Physics"..."Total", "Exit". I would probably make an enum for this, but that is overkill for a homework assignment.
The loop gets moved to the body of the script.
Now that max_marks does not take input and returns output you can write a doctest. You also have a function that can be used by other scripts/programs.
get_input() displays a prompt, gets user input, VERIFIES USER INPUT, and returns user input. I would write it to return something more friendly than 1, 2, 3... Perhaps "Math", "Physics"..."Total", "Exit". I would probably make an enum for this, but that is overkill for a homework assignment.
The loop gets moved to the body of the script.
while True: choice = user_input() if choice == "Exit": break elif choice == "Total": totmax, max_marks = total_marks(Total_marks) print(f'Highest total score: {totmax} - {max_marks}') else: high_mark, student = max_marks(total_marks[choice], Students) print(f'Highest {choice} score: {student} - {high_mark}')This makes the assumption that "total_marks" is a dictionary of all of the marks and that the keys are "Math", "Physics"...
Now that max_marks does not take input and returns output you can write a doctest. You also have a function that can be used by other scripts/programs.