Python 8a Notes - updated 2 (1)
Python 8a Notes - updated 2 (1)
1 of 22
Contents
The payment.txt should be a text file storing data with the following format:
P. 3 of 22
1. View Payments:
➢ Displays all existing credit card payment records in a clear and
organized format.
➢ Provides an overview of the current financial commitments.
4. Delete a Payment:
➢ Enables users to delete a specific credit card payment record.
➢ Includes a confirmation step to prevent accidental deletion, with
proper warnings for invalid inputs.
How It Works:
➢ The program dynamically loads data from payment.txt at the start and saves
changes to the file upon exit.
➢ A menu-based interface allows users to interact with the system through
simple numeric choices.
➢ Input validation ensures that only valid data is accepted, improving reliability
and user experience.
➢ The system clears the screen between actions for a clean and professional
look.
P. 4 of 22
The tracker will start by providing a menu for user to opt for handling minimum
payment.
P. 5 of 22
Notice that we have to set the system only allows user to input credit card name in a
maximum of 7 characters.
P. 7 of 22
Notice that the system should prevent using from any empty input of credit card
name.
P. 8 of 22
Extra requirement for entering record – Credit card name can’t be found
Notice that the system should notify user in case the credit card name can’t be
found.
P. 9 of 22
1. If the credit card name provided by user can’t be found in the system, the
user should be noticed by this issue:
2. If user input something other than y/n or Y/N, they will be warned as shown
below:
P. 11 of 22
Basic Syntax
Embedding Expressions
Specifying Formats
Multi-line f-strings
You can use f-strings across multiple lines by enclosing them in triple quotes:
Escaping Braces
Elaboration of creditcard.py
As we have learned the way to use f-strings, let’s walk through the creditcard.py
Python program. It is the one focusing on code structure, syntax, and functionality.
1. Importing Modules
import os
➢ os.path.abspath(__file__):
✓ Gets the full absolute path of the current script.
➢ os.path.dirname():
✓ Extracts the directory path of the file from the absolute path.
➢ os.path.join(CURRENT_PATH, "payment.txt"):
✓ Combines the directory path (CURRENT_PATH) with the filename
payment.txt to create a full file path.
➢ Function definition:
✓ def clear_screen():: Defines a function named clear_screen
with no parameters.
➢ os.system('cls'):
✓ Executes the system command cls (clear screen) on Windows.
P. 16 of 22
A. Function Definition:
➢ def load_data()::
Begins the definition of the load_data function, which loads credit
card payment data from a file.
C. Open File:
➢ with open(FILE_NAME, "r") as file::
✓ Opens the file in read mode ("r"). The with statement
ensures the file is properly closed after reading.
➢ file.readlines():
✓ Reads all lines of the file into a list called lines.
P. 17 of 22
data dictionary
“AE
key “BOC” “HSBC” “DBS” “ICBC” “HSB”
Card”
A. Function Definition:
➢ def save_data(data)::
Defines a function to save the dictionary data into the file.
A. Function Definition:
➢ def display_menu()::
Defines the function that displays the main menu options.
B. print Statements:
➢ Each print() displays a menu option for the user to choose from.
P. 19 of 22
7. View Payments
def view_payments(data):
clear_screen()
if not data:
print("No payments found.")
else:
print("\nCurrent Payments:")
for card, amount in data.items():
print(f"{card}: {amount}")
input("\nPress Enter to return to the menu...")
A. Clear Screen:
➢ clear_screen():
Clears the terminal before displaying payments.
C. Display Data:
➢ for card, amount in data.items()::
Loops through the dictionary and prints each card name and its
amount.
B. Validation Checks:
➢ If the card name is empty (if not card), an error message is
displayed.
➢ If the card name exceeds 7 characters (len(card) > 7), another
error message is displayed.
Add Payment:
➢ Prompts the user to enter a new card name and minimum payment.
➢ Checks if the card already exists and displays an error if it does.
Update Payment:
➢ Allows the user to modify the payment amount for an existing card.
Delete Payment:
➢ Prompts the user to confirm before removing a card from the dictionary.
➢ Menu Loop:
✓ Uses while True to keep displaying the menu until the user chooses to
exit (choice == "5").
➢ User Input:
✓ Prompts the user to select an option with input().
➢ Option Handling:
✓ Each menu option is handled with an if-elif block.