12860 - Python2020_MOOCS_Practice_Problem4   

Description

Write a program to record the daily expense.
Use a data structure (e.g. a list of tuples) to keep the records of expense, date, and description.
Define 3 functions: write_recordlist_records, and total_expensewrite_record writes to the data structure, list_records prints out the records, and total_expense returns the total expense.

The function write_record should accept 3 parameters, representing expense, date, and description. Date is an optional parameter with default value being "2020/01/18". Description is also an optional parameter with default value of being "something".

The function list_records has no return value. It should print out all the records, one in a line, in the format as "$100, 2020/01/18, lunch".

The function total_expense returns the total expense, in the type of integer.

Example usage:
>>> write_record(40, '2020/01/16', 'breakfast')
>>> write_record(99, date='2020/01/17')
>>> write_record(300, description='book')
>>> write_record(55)
>>> list_records()
$40, 2020/01/16, breakfast
$99, 2020/01/17, something
$300, 2020/01/18, book
$55, 2020/01/18, something
>>> print(total_expense())
494

[NOTE]
For judging purpose, the code you submit should take 4 strings representing the function calls of write_record from input and execute the function calls using the built-in function exec.
Then call list_records and print the returned value from total_expense at the end.
In short, you should use the following template code and fill in the required parts.

records = # YOUR CODE to initialize the data structure
def write_record(...): # YOUR PARAMETERS
    # YOUR CODE to write to records
def list_records():
    # YOUR CODE to print the records
def total_expense():
    # YOUR CODE to return the total expense

for i in range(4):
    calling_write_record = input()
    exec(calling_write_record)
list_records()
print(total_expense())

 

Input

4 lines of string representing the function call of write_record.

Output

4 lines of record printed by list_records() and 1 line of the total expense.

Sample Input  Download

Sample Output  Download




Discuss