Question: this program needs to be edited according to the question mentioned in bold: import os.path def tag_index(tag,lines): i = 0 found = False line=-1 while

this program needs to be edited according to the question mentioned in bold:

import os.path def tag_index(tag,lines): i = 0 found = False line=-1 while i < len(lines) and not found: line = lines[i] if line.strip().startswith(tag): line = i # line index found = True else: i += 1 return line

def get_lines(): 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 file = open(filename, encoding="utf-8") lines = file.readlines() file.close() return lines def main(): lines = get_lines() # pull out the header data start_tag = "

" # Start tag end_tag = "

" # End tag start_line=tag_index(start_tag,lines) end_line=tag_index(end_tag,lines) h_lines = lines[start_line + 1 : end_line] _, name = h_lines[0].strip().split(':') _, status = h_lines[1].strip().split(':') # pull out the data block start_tag = "" # Start tag end_tag = "" # End tag start_line=get_index(start_tag,lines) end_line=tag_index(end_tag,lines) data_lines = lines[start_line + 1 : end_line] 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()

Q6: Extract and use a second function tagged_contents(tag_name, lines), which returns the lines between the start and end tags of a block. For instance:

lines = ["not included", "", "included", "also included", "", "not included also" ] 
tagged_contents("data", lines)

will return the list:

["included", "also included"] 

Test your modified program still works, then copy and paste it into the answer box. It must use the function tagged_contents where appropriate.

Note:

  • All functions must now be less than 39 lines long
  • Make sure that you include and use the function tag_position from the last question

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 ----------------------------------------
# ignoring main, testing tagged_contents function lines = [ "junk", "junk", "", "Louise", "Katia", "George", "Eiran", "Patrick", "George", "Matthew", "Lucy", "Elizabeth", "", "junk" ] names = tagged_contents("names", lines) print(len(names)) if names != lines[3:-2]: print("Contents are not returned correctly") print(names)

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 done

THE QUESTION NUMBER 6 THAT SAYS TO EDIT THE PROGRAM IS THE QUESTION.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!