Tip Calculator Project (Code) in Python

← Back to Projects

Tip Calculator in Python

About the project:This Python script is a simple, command-line tip calculator. It guides you through entering the bill amount, the tip percentage, and the number of people splitting the bill. It then provides a clear summary of all the calculated values.

How to use this Calendar Generator:

  • Run the Python script.
  • You can run this script, and it will continue to prompt for new calculations until you choose to quit.


Project Level: Beginner

You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.

Steps: Follow these steps

Step 1: Copy below code using green 'copy' button.

Step 2: Paste the code on your chosen editor.

Step 3: Save the code with filename and .py extention.

Step 4: Run (Press F5 if using python IDLE)




# tip_calculator.py

def get_numeric_input(prompt, min_value=0):
    """
    Helper function to get valid numeric input from the user.
    Ensures the input is a positive number.
    """
    while True:
        try:
            value = float(input(prompt).strip())
            if value <= min_value:
                print("Please enter a positive value.")
            else:
                return value
        except ValueError:
            print("Invalid input. Please enter a number.")

def calculate_tip():
    """
    Calculates the tip and bill split based on user input.
    """
    print("--- Python Tip Calculator ---")

    while True:
        # Get the total bill amount from the user
        bill_amount = get_numeric_input("Enter the total bill amount: $", min_value=0)
        
        # Get the desired tip percentage
        tip_percentage = get_numeric_input("Enter the tip percentage (e.g., 15 for 15%): ", min_value=0)
        
        # Get the number of people to split the bill
        num_people = int(get_numeric_input("Enter the number of people to split the bill: ", min_value=1))

        # Calculate the tip amount
        tip_amount = bill_amount * (tip_percentage / 100)
        
        # Calculate the total bill including tip
        total_bill_with_tip = bill_amount + tip_amount
        
        # Calculate the amount each person should pay
        amount_per_person = total_bill_with_tip / num_people

        # Display the results
        print("\n--- Calculation Summary ---")
        print(f"Bill Amount: ${bill_amount:.2f}")
        print(f"Tip Percentage: {tip_percentage}%")
        print(f"Tip Amount: ${tip_amount:.2f}")
        print(f"Total Bill with Tip: ${total_bill_with_tip:.2f}")
        print(f"Amount per Person: ${amount_per_person:.2f}")
        print("---------------------------\n")

        # Ask if the user wants to perform another calculation
        play_again = input("Do you want to perform another calculation? (yes/no): ").strip().lower()
        if play_again != 'yes':
            print("Exiting Tip Calculator. Goodbye!")
            break

# This ensures that calculate_tip() is called only when the script is executed directly.
if __name__ == "__main__":
    calculate_tip()