Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Question 1 Write a function called readFile ( ) which takes as a parameter a string representation of a file and returns a list of
Question
Write a function called readFile which takes as a parameter a string representation of a file
and returns a list of lists containing the information found in the file.
The file that you will read contains banking information in the following format:
date type amount
debit
credit
debit
The first line contains a header date type amount This should not be included in your
data. Some hints for excluding this line if you see the word "date", don't process this line or if
you are using the readline method, ignore the first call to readline and call it again to read the
second line.
The next lines contain information for bank transactions occurring in April The first value
is the day of the month an integer the second value will be either the string "debit" that is a
dollar amount deducted from the account or "credit" that is a dollar amount added to the
account The third value is a floating point value representing a dollar amount.
You will read the data from the file in your function and put the data into a list of lists in the
following format:
"debit", "credit",
Your data types must be appropriate that is the day of the month should be an integer, the type
of transaction should be string and the amount should be a float
Your function should return the list of lists.
Your function should print an error message that reads "File error" and return if there is an
error encountered when opening or reading the data from the file.
You do not need to write a docstring for this function, but inline comments should be
included where appropriate.
Question
Write a function called "calculateBalance" that given an initial account balance, a list of
transactions, and a day of the month will return the balance before the given day of the
month. The first parameter will be an account balance a number The second parameter will
be a list of lists of the format day type, amount where day is an integer representing a day of
the month, type is a string one of "credit" or "debit" and amount is a floating point value
representing the amount of the credit or debit. The list of lists may look something like
this: "credit", "debit", where the two sublists represent two banking
transactions one being a credit on the first day of the month for $ and the second being a
debit on the second day of the month for $ The third parameter to this function will be an
integer representing a day of the month.
The function will apply the transactions as given in the second parameter to the initial balance
the first parameter up to and not including the specified day of the month the third
parameter and it will return the calculated balance. Remember, credits add to the balance,
debits are subtracted from the balance.
You may assume that the list of transactions is sorted by day of the month. You may also
assume that the data passed into the function is valid that is the parameters are of the correct
data types and fall within a reasonable range Your function should not do additional
work. That is once you have reached the given day of the month, processing should
stop. Because the transactions are in order, you do not need to continue to look at transactions
past the specified date.
Your function MUST work for any list of lists, not just for the data that is read from the file,
although you can assume that you will have only ONE month's data in the list of lists. The list of
transactions could be empty. Be sure that your function works in this case.
To further clarify, consider the following examples:
calculateBalance "credit", "debit",
calculateBalance "credit", "debit", "debit",
calculateBalance "credit", "debit", "debit",
calculateBalance "credit", "debit", "debit",
calculateBalance
This function should NOT call the readURL function written in question
You do not need to write a docstring for this function, but inline comments should be
included where appropriate.
Question
Write a function called userInput to interact with the user to collect a series of integers
representing the day of the month and account balances floating point You will ask for
day balance pairs which will be stored as a list of tuples. So the values will look something
like this: where the list contains tuples. Each
day of the month must be between and and each balance must be greater than zero. You
must check that the values entered by the user are within these ranges and, if not, repeatedly
prompt the user for a valid input. You m
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