Python Forum
Doctesting a function which prints a students name along with the maximum mark scored
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Doctesting a function which prints a students name along with the maximum mark scored
#5
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.
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.
Reply


Messages In This Thread
RE: Doctesting a function which prints a students name along with the maximum mark scored - by deanhystad - Jan-30-2022, 01:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  i scored 1/8 on python assessment where did i mess up? SAVAGEMIKE 5 684 Jan-18-2024, 02:50 PM
Last Post: Pedroski55
  zfill prints extra et the end of a var tester_V 4 915 Mar-24-2023, 06:59 PM
Last Post: tester_V
  Mark outlook emails as read using Python! shane88 2 6,580 Feb-24-2022, 11:19 PM
Last Post: Pedroski55
  variable prints without being declared. ClockPillow 2 1,822 Jul-11-2021, 12:13 AM
Last Post: ClockPillow
  python prints none in function output chairmanme0wme0w 3 2,236 Jul-07-2021, 05:18 PM
Last Post: deanhystad
  Something wrong with the quotation mark in dictionary definition Mark17 1 2,005 Jan-29-2021, 03:34 PM
Last Post: buran
  Output prints Account.id at the end? LastStopDEVS 5 2,801 Dec-19-2020, 05:59 AM
Last Post: buran
  Try/Exept prints only ones tester_V 11 3,911 Nov-03-2020, 02:38 AM
Last Post: tester_V
  How to mark duplicate rows in pandas Mekala 3 2,571 Sep-17-2020, 11:32 PM
Last Post: scidam
  loop only prints last character. mcmxl22 1 1,735 Feb-17-2020, 02:36 AM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020