Question
In python 3 You are asked to write a program that reads a file with accounts then allows you to processes some accounts by name
In python 3
You are asked to write a program that reads a file with accounts then allows you to processes some accounts by name and adding amounts. You will need to catch exceptions and handle them. To do this, you need to write two functions: readAccounts(infile) to read the account file, and processAccounts(accounts) to make changes to accounts.
In the main function of your program the user will be asked to enter the name of the file to read. If the file does not exist, the main function should be able to catch the IOError exception. Upon catching the exception, the main function should print an error message and exit the program gracefully (i.e. no crash with call stack displayed). Here is a sample run that shows what happens when the filename entered by the user does not exist:
Enter filename> sillyfile.txt
IOError. Sillyfile.txt does not exist
If the file exists, the main function opens the file in read mode and proceeds to call the function readAccounts(). The main function passes the TextIOWrapper object (the file handler) as an argument to this function.
Each line in the file is a record of a persons name followed by a colon symbol followed by a number that represents the amount the person has in their account. The function readAccount reads the file one line at a time and stores the name:account as a key:value pair inside a dictionary. If the amount value associated to a name is not a valid number the function should raise an exception and that name:amount pair with the invalid amount, is not added to the dictionary. If an exception is raised, the following message is printed by the function:
ValueError. Account for Smith not added: illegal value for balance
After raising and catching the exception the program should continue to the next record in the file.
Once all records with valid amounts have been added into the dictionary, the readAccounts function should return this dictionary.
The main function should now call the processAccounts() function and pass the dictionary that was returned by readAccounts() as an argument. The processAccounts() function should ask the user to enter the name of a person. If the entered name does not exist as a key in the dictionary, a KeyValue Error is raised and should be caught by the program. For example is AliBaba is given as a name and these no such key in the dictionary, the following message is displayed :
KeyError. Account for AliBaba does not Exist. Transaction cancelled.
After catching the exception and handling it, the function asks the user to enter name again. If the name exists in the dictionary, the function asks the user to enter the amount value that would be added to the existing amount associated to the name in the dictionary. If the user enters a value that is not a float or integer, the function raises and catches a ValueError exception and the following message is displayed:
Value Error. Incorrect Amount. Transaction cancelled.
After catching and handling the exception, the program allows the user to continue entering and new name of an account holder and the transaction amount until the user enters the word Stop. If the name exists in the accounts dictionary and the amount is a valid integer or float value, the transaction goes through and the function reports the amount in the account after been updated. Please note you dont have to update or write to original file. You just need to update the amount in the dictionary. Here is a complete sample run of the program:
Enter filename >accounts.txt
ValueError. Account for Smith not added: Illegal value for balance
ValueError. Account for George not added: Illegal value for balance
Enter account name, or 'Stop' to exit: bob
KeyError: Account for bob soes not Exist. Transaction cancelled.
Enter account name, or 'Stop' to exit: Bob
Enter transaction amount for Bob: 30
New balance for account Bob: 264.7
Enter account name, or 'Stop' to exit: Zoya
Enter transaction amount for Bob: hello
Value Error. Incorrect Amount. Transaction cancelled.
Enter account name, or 'Stop' to exit: Johnson
Enter transaction amount for Johnson: -20
New balance for account Bob: 771.56
Enter account name, or 'Stop' to exit: Stop
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started