Answered step by step
Verified Expert Solution
Link Copied!

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 1
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
1 debit 300.00
3 credit 200.00
5 debit 500.00
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 2021. 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:
[[1, "debit", 300.00],[3, "credit", 200.00]...]
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 -1 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 2
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: [[1, "credit", 100.00],[2, "debit", 200.50]] where the two sub-lists represent two banking
transactions one being a credit on the first day of the month for $100 and the second being a
debit on the second day of the month for $200.50. 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(1000,[[1, "credit", 100.00],[3, "debit", 200.00]],2)-->1100
calculateBalance(500,[[1, "credit", 100.00],[3, "debit", 200.00],[10, "debit", 500.00]],6)-->
400
calculateBalance(500,[[1, "credit", 100.00],[3, "debit", 200.00],[10, "debit", 500.00]],10)-->
400
calculateBalance(500,[[1, "credit", 100.00],[3, "debit", 200.00],[10, "debit", 500.00]],11)-->-
100
calculateBalance(500,[],31)-->500
This function should NOT call the readURL() function written in question 1.
You do not need to write a docstring for this function, but inline comments should be
included where appropriate.
Question 3
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 10
(day, balance) pairs which will be stored as a list of tuples. So, the values will look something
like this: [(10,200.00),(29,1000.00)....(5,500.00)] where the list contains 10 tuples. Each
day of the month must be between 1 and 31 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

Have a brief review of human motivation theories

Answered: 1 week ago