Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Code written by Harry Low on the 19th May 2019.
- Prints peoples names' and the status of their account and donations to
- each band."""
- import os.path
- def tag_position(tag, lines):
- """Provides the index position of a tag within certain lines of code"""
- counter = 0
- line_index = 0
- tag_found = False
- while counter < len(lines):
- if lines[counter].strip().startswith(tag):
- line_index = counter
- tag_found = True
- counter += 1
- else:
- counter += 1
- if tag_found == False:
- return -1
- else:
- return line_index
- return line_index
- def tagged_contents(tag_name, lines):
- """Returns the lines of data between specified start and end tags in file"""
- def main():
- """Iterates through text files to retrieve data related to donation status.
- Prints data and analsyses band names starting and ending with tags and returns
- amount donated to each band"""
- 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_data = open(filename, encoding="utf-8")
- lines = file_data.readlines()
- file_data.close()
- end_head = tag_position("</head>", lines)
- start_head = tag_position("<head>", lines)
- header_lines = lines[start_head+1:end_head]
- _, donor_name = header_lines[0].strip().split(':')
- _, donor_status = header_lines[1].strip().split(':')
- start_data = tag_position("<data>", lines)
- end_data = tag_position("</data>", lines)
- data_lines = lines[start_data+1:end_data]
- data = []
- for line in data_lines:
- _, band_name, amount = line.strip().split(',')
- amount = float(amount)
- data.append((band_name, amount))
- total_donations = 0
- for band_name, amount in data:
- total_donations += amount
- print("-" * 40)
- print("Donation Summary")
- print("-" * 40)
- print("User name: {}".format(donor_name.title()))
- print("Account Status: {}".format(donor_status))
- if donor_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_donations))
- print("-" * 40)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement