223k views
3 votes
The program in "proj01b.py" will display one summary report to the user. a) The program will prompt the user to enter the name of the input file. If it is unable to open that file, the program will prompt the user again until the user enters a valid file name. b) The program will prompt the user to enter a year, and will then prompt the user to enter an income level. The income level must be one of the characters in the set 1,2,3,4, where 1 corresponds to "low income", 2 corresponds to "lower middle income", 3 corresponds to "upper middle income" and 4 corresponds to "high income". c) 'The prognam will identify all records (lines) in the input file which match the user's criteria for year and income level, and the program will display a report with the following information: - The count of records in the input file which match the user's criteria - The average percentage for those records (displayed with one fractional digit) - The country with the lowest percentage for those records - The country with the highest percentage for those records The name of the country and the percent of children vaccinated will be displayed for the last two items (lowest percentage and highest percentage). 2. The program will display appropriate messages to inform the user about any unusual circumstances. 3. The program will contain at least three functions.

1 Answer

6 votes

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.

User Jorjdaniel
by
8.2k points