Question: Q5 : The major flaw with the program provided by Give Music is that it repeatedly reuses similar sections of code without bothering to extract
Q5 : The major flaw with the program provided by Give Music is that it repeatedly reuses similar sections of code without bothering to extract that code into functions.
Your task here is to extract from the program a function tag_position(tag, lines) that returns the index of the given tag in the given list of lines. For example tag_position("
", lines) will return 0 for all the test files that you have been provided. If the tag is not in the file, then -1 should be returned (This will be useful for question 8 :-) ).You should then replace all occurrences of the extracted code with calls to tag_position.
Test that your program still behaves as before, then submit the entire program in the answer box below.
Notes
- Your program should now have at least two function definitions, tag_position and main. If you have defined other helper functions then they should, of course, be included.
- The earlier style checks have been tightened slightly and function definitions must contain at most 45 lines/statements per function.
This is the code that need to be changed for each question:
"""main doc""" import os.path def main(): """dd""" filename = None while filename is None: filename = input("Enter a file name: ") if not os.path.isfile(filename): print(filename, "DOES NOT EXIST!") filename = None first = open(filename, encoding="utf-8") lines = first.readlines() first.close() # pull out the header data start = "
" # Start tag end = "" # End tag i = 0 found = False while i < len(lines) and not found: lenght = lines[i] if lenght.strip().startswith(start): startline = i # Start line index found = True else: i += 1 i = 0 found = False while i < len(lines) and not found: lemon = lines[i] if lemon.strip().startswith(end): endline = i # End line index found = True else: i += 1 h_lines = lines[startline+1:endline] _, name = h_lines[0].strip().split(':') _, status = h_lines[1].strip().split(':') # pull out the data block start = "" # Start tag end = "" # End tag i = 0 found = False while i < len(lines) and not found: lemon = lines[i] if lemon.strip().startswith(start): startline = i # Start line index found = True else: i += 1 i = 0 found = False while i < len(lines) and not found: lemon = lines[i] if lemon.strip().startswith(end): endline = i # End line index found = True else: i += 1 data_lines = lines[startline+1:endline] data = [] for line in data_lines: _, band_name, amount = line.strip().split(',') amount = float(amount) data.append((band_name, amount)) total = 0 for band_name, amount in data: total += amount # Print the report print("-" * 40) print("Donation Summary") print("-" * 40) print("User name: {}".format(name.title())) print("Account Status: {}".format(status)) if status in ["suspended", "deleted"]: print("*** WARNING ***") print("*** User can't access their account ***") print("-" * 40) for band_name, amount in sorted(data): print("{} ({:.2f})".format(band_name, amount)) print("-" * 40) print("Count: {}".format(len(data))) print("Total: {:.2f}".format(total)) print("-" * 40) main()For example:
| Test | Input | Result |
|---|---|---|
| | testfile0.txt | Enter a file name: testfile0.txt ---------------------------------------- Donation Summary ---------------------------------------- User name: Lilly Lyvers Account Status: active ---------------------------------------- Black Flag (900.00) Death Cab for Cutie (32.00) Dixie Chicks (72.25) Fugazi (85.75) One Direction (37.00) Poco (6.00) Portishead (87.15) Red Hot Chili Peppers (90.00) Television (86.50) The Drifters (96.00) The Jam (90.00) The Yardbirds (73.00) ZZ Top (24.00) ---------------------------------------- Count: 13 Total: 1679.65 ---------------------------------------- |
# ignores your call to main # and tests isyour tag_position function file = open("testfile0.txt") lines = file.readlines() file.close() print(tag_position("", lines)) print(tag_position("", lines)) print(tag_position("", lines)) print(tag_position("", lines)) | | 0 3 4 18 |
the test file:
testfile0
testfile0:
# example of a comment that we can make name:Lilly Lyvers account status:active # here is another one I guess # hey look the data is starting 2016-01-30,Death Cab for Cutie,32.0 2016-05-03,Portishead,87.15 2016-06-09,Dixie Chicks,72.25 2016-08-02,The Yardbirds,73.0 2016-10-25,The Drifters,96.0 2017-06-15,Television,86.5 2017-06-27,One Direction,37.0 2017-11-16,Black Flag,900.0 2018-01-16,The Jam,90.0 2018-03-27,Fugazi,85.75 2018-05-07,ZZ Top,24.0 2018-08-01,Poco,6.0 2018-11-23,Red Hot Chili Peppers,90.0 # And we are doneStep by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
