Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Explain what is going on in the following program. def main (file_name): \'\'\'

ID: 3816114 • Letter: E

Question

Explain what is going on in the following program. def main (file_name): ''' >>> main ('') File named not found >>> main ('x') Traceback (most recent call last): ... raise IOError ('bad file format.') IOError: bad file format. >>> main ('player-career. csv') The top 10 players in efficiency are ******************** Wilt Chamberlain: 41.50 Bill Russell: 31.71 Oscar Robertson: 31.61 Bob Pettit: 31.11 Kareem Abdul-Jabbar: 30.93 Larry Bird: 29.77 Elgin Baylor: 29.74 Michael Jordan: 29.19 Magic Johnson: 29.10 Charles Barkley: 28.16, , try: nba_file - open (file_name) except IOError: print ('File named {} not found'. format (file_name)) else: nba_dict-{} line_str - nba_file. Readline() if line_str[0:5]!-' ilkid': raise IOError ('bad file format.') for line_str in nba_file: calc_efficiency (line_str, nba_dict) results_list - find_most_efficient (nba_dict, 10) print_results (results_list) nba_file.close() if_name_ --'_main_': import doctest doctest.testmod() What is the purpose of doctest and how does it work?

Explanation / Answer

A)

Give code is a function to find top 10 efficient nba players.
It also include doctest which contains three test cases.
step 1: Tries to open given file name, if file doesnot exist it error out. IO ERROR
step 2: checks format of file, if it is not matching then it error out. IO ERROR
step 3: for each player it calculate efficieny and update it in dictionary calls function
step 4: find 10 efficient player and prints result.

doctest have three test cases.
1) test with empty file name.
2) test with file name whose format is not correct.
3) test with file players_career.csv


B) The doctest module searches for pieces of text that look like interactive Python sessions,
and then executes those test cases. There are several common ways to use doctest:

Purpose of doctest are following:

1) To check module example are uptodate, and working as expected.
2) To perform unit testing by verifying that test object work as expected.
3) To write an example for documentation.

The docstrings are strings in function part under """ """ which is commented part.
when we run it with -v flag it scans the commented part, for interactive session examples
and tests each one by one.
for example:
def factorial(n):
    """
     >>> [factorial(n) for n in range(6)]
    [1, 1, 2, 6, 24, 120]
    """
    result = 1
    factor = 2
    while factor <= n:
        result *= factor
        factor += 1
    return result

  
    here first line is input and rest is expected output.
    These input will be run by function and compare with expected output.
    One by one it tests all test cases and compares the result.
  
  

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote