Question
The goal of this lab is to practice writing code using Exceptions, Bounded Queues, Circular Queues and Single Linked List. In this program you must
The goal of this lab is to practice writing code using Exceptions, Bounded Queues, Circular Queues and Single Linked List.
In this program you must write three functions. Here is the definition of the three functions:
def main()
def readAccounts(infile)
def processAccounts(accounts)
in the main function the user is asked to enter the name of the file. If the file does not exist, the main function should raise an exception. If raised, the main function should be able to catch the IOError exception. Upon catching the exception, the main function prints an error message and exits the program. Here is a sample run that shows what happens when the filename entered by the user does not exist:
Enter filename >doesnotexit.txt
IOError.Input file does not exist
If the file exists, the main function opens the file in read mode and proceeds to call a function readAccounts. The main function passes the TextIOWrapper object (the file handler) as an argument to this function.
Each line in the file 'accounts.txt' 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.
Bob:234.70 Johnson:791.56 Anna:3260.55 Jill:-23.76 Smith:Not_A_Float! James:1022.71 Sarah:-567.30 George:eleven Vanessa:43781.43 Zoya:417.99
The function readAccount reads the file one line at a time and stores the name:account as a key:value pair inside the dictionary. If the amount value associated to a name is not a valid number the function should raise an exception and that name:amount 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 to this function. This function should ask the user to enter the name of a person. If the name does not exist in the dictionary a KeyValue Error is raised and caught by the program. The following message is displayed in this case:
KeyError.Account does not Exist.Transaction cancelled.
After catching the exception, 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 the exception, the program allows the user to continue entering name of the account holder and the transaction amount until the user enters 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 has been updated. Please note you dont have to update or write to accounts.txt file. You just need to update the amount in the dictionary.
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