Skip to content

Lab - More Functions and Conditionals

This activity is based wholly on a lab written by Prof. Alvin Chao.

In this lab, you will practice writing functions. You will submit to gradescope, but you do not need to write docstrings or comply with flake8. Your code will only be graded on the correctness of the functions.

You will develop a set of functions for a system that calculates the payment for a credit card. There are more functions than needed - just to practice. However, this is a realistic simulation of how credit cards work.

STEP 1 - Previous Balance (the balance currently being carried on the card)

Write a function called check_previous_balance that takes a single float parameter which represents a customer's previous credit card balance. If the previous balance value is not between -5000 and 50000, assume the balance is 0. Return the balance.

STEP 2 - Additional Charges (the charges made this past month)

Write a function called check_new_charges that takes a single float parameter which represents the money charged by a credit card customer during the past month. If the new charges value is not greater than 0, assume the additional charges are 0. Return the value.

STEP 3 - Compute the interest owed

Write a function called compute_interest that takes the previous balance and new charges as (float) parameters and returns the interest. Calculate the interest according to the rules:

If the previous balance is not greater than 0, the interest is 0. Otherwise the interest is 13% of the total of the previous balance and the new charges.

STEP 4 - Compute the minimum payment due this month

Write a function called minimum_payment that takes the previous balance, new charges and interest as float parameters and returns the new balance and the minimum payment due amount. Calculate the minimum payment according to the rules:

Minimum Payment Rule
$0.00 if the new balance is less than $ 0.00
The full balance if the new balance is between 0.00 and 49.99 (inclusive)
$50.00 if the new balance is between 50.00 and 300.00 (inclusive)
20% of the balance if the new balance is over $300.00

Return the new balance amount first, followed by the minimum payment, in a single tuple.

STEP 5 - Complete the main function

Copy and paste the main function given below into your program and add the missing code in the places where the comments tell you TODO. Look at the print statements given to know what to name your variables.

Then run and test the full program by running the main function.

def main():
    prev_balance = float(input("Enter the previous balance: "))

    # TODO: write a call to your check_previous_balance function to adjust
    # the user-entered previous balance value, if needed


    new_charges = float(input("Enter the new charges for this month: "))

    # TODO: write a call to your check_new_charges function to adjust the
    # user-enrtered new charges amount, if needed


    # TODO: write a call to your compute_interest function to get the interest


    # TODO: write a call to your minimum_payment function to calculate the
    # new balance and the minimum payment due
    # return the 2 values into a variable called values


    # output the resulting credit card statement
    print("")
    print("CS CARD International Statement")
    print("===============================")
    print(f"Previous balance:    $ {prev_balance:8.2f}\n")
    print(f"Additional charges:  $ {new_charges:8.2f}\n")
    print(f"Interest:            $ {interest:8.2f}\n")
    print(f"New balance:         $ {values[0]:8.2f}\n")
    print(f"Minimum payment:     $ {values[1]:8.2f}\n")

BONUS - calculate the new balance after the user makes a payment

Edit the main function to also print what the balance will be the following month if the person only pays the minimum payment due.

Current interest rates on credit cards are about 20% and minimum required payments are usually much less than what we programmed. Is it a good idea to carry a balance on a credit card??

STEP 6 - Submit into gradescope

Submit your program in a file called credit_card.py. Be sure that any testing code that you wrote, including the call to the main function, is inside a if __name__ == '__main__' block.