代做Exam Questions Winter 2021帮做Python语言

2024-06-17 代做Exam Questions Winter 2021帮做Python语言

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 may assume that the user enters integers for the days and floating point values for the amounts (not strings or other data types).

Duplicate days and amounts are allowed.

Return the list of tuples. Your function should take no parameters.

Commenting:  This function MUST have a docstring (function comment) and inline comments. 3 marks will be allocated to proper commenting for this question.

Question 4

This is where you will put the program together.  ALL code from the above functions should be included in this program and pasted together with the code that you write in this question into the text box below. We should be able to cut and paste the code from this question into an IDLE editor and run your program. When you are finished with your program, upload a copy of the file using the add File button in this question.

You can still do this  question even if the code in questions 1, 2 and/or 3 does not work.  Take the following action if one or more of the functions don't work in your code:

Question 1:  if you were unable to read the information from the website, comment out your code and add the following to the end of your function:

return [[1, "debit", 300.00], [3, "credit", 200.00], [4, "credit", 500]]

Question 2: If you were unable to get the second function to run, comment out your code and add the following to the end of your function:

return 200

Question 3: If you were unable to get the third function to run, comment out your code and add the following to the end of your function:

return [(2, 100.00), (21, 1000.00), (12, 1500.00)]

Complete your program so that it first reads the transaction data from the file and stores it. Next,  provides the user with a menu containing 4 options.  The first option is to print the transaction data in a tabular format like this:

1 credit 300.00

2 debit   500.00

4 credit  50.00

You will need to write the code to do this.  Structure your program appropriately. If you need another function, please feel free to add one (or more).

The second option is to prompt the user for 10 day/account pairs. You have already written a function that will provide this functionality (userInput()).

The third option will be to find the new balances given the ten (day, current balance) pairs obtained from the user in option 2 and the transaction data read from the website. (If the user hasn't yet selected option 2, inform. them that they need to select option 2 first before running this option). For each of the 10 day/balance pairs, you will run the calculateBalance() function with the appropriate data to find the current balance after all the transactions were applied up to (but not including) the given day.  Make your program user friendly -- that is let the user know what the current balance is and what the new balance was on day X.

The fourth option is to quit the program.  When they choose this option, print "Have a nice day" and end the program.

Your program must not have any global variables.

You may assume that the user enters an integer value for a menu choice, but you need to check that it is a valid number (between 1 and 4).  If not, prompt the user repeatedly for a new value.

Your program should allow the user to repeatedly choose from the menu options until they choose quit, but it should only read the data from the website at most once. The options should be clearly stated.