Here's an example implementation of the "proj01b.py" program in Python that meets the given requirements:
```python
def open_file():
while True:
try:
filename = input("Enter the name of the input file: ")
file = open(filename, 'r')
return file
except FileNotFoundError:
print("Invalid file name. Please try again.")
def get_user_criteria():
year = int(input("Enter a year: "))
while True:
income_level = input("Enter an income level (1=low, 2=lower middle, 3=upper middle, 4=high): ")
if income_level in ['1', '2', '3', '4']:
return year, income_level
else:
print("Invalid income level. Please try again.")
def process_records(file, year, income_level):
count = 0
total_percentage = 0
lowest_percentage = float('inf')
lowest_country = ""
highest_percentage = float('-inf')
highest_country = ""
for line in file:
record = line.strip().split(',')
record_year = int(record[0])
record_income_level = record[1]
percentage = float(record[2])
if record_year == year and record_income_level == income_level:
count += 1
total_percentage += percentage
if percentage < lowest_percentage:
lowest_percentage = percentage
lowest_country = record[3]
if percentage > highest_percentage:
highest_percentage = percentage
highest_country = record[3]
return count, total_percentage / count, lowest_country, highest_country
def display_report(count, average_percentage, lowest_country, highest_country):
print("Summary Report")
print("==============")
print("Count of matching records:", count)
print("Average percentage: {:.1f}".format(average_percentage))
print("Country with lowest percentage:", lowest_country)
print("Country with highest percentage:", highest_country)
def main():
file = open_file()
year, income_level = get_user_criteria()
count, average_percentage, lowest_country, highest_country = process_records(file, year, income_level)
display_report(count, average_percentage, lowest_country, highest_country)
file.close()
# Entry point of the program
if __name__ == "__main__":
main()
```
In this implementation, the `open_file()` function handles the prompt for the input file name and ensures that a valid file is opened. The `get_user_criteria()` function prompts the user for the desired year and income level. The `process_records()` function reads each line of the input file, filters the records based on the user's criteria, and calculates the count, average percentage, lowest percentage, and highest percentage. Finally, the `display_report()` function prints the summary report to the user. The `main()` function orchestrates the execution of the program by calling the other functions in the appropriate order.
Make sure to replace the necessary parts with your specific implementation logic and file structure.