Make micro Python programs to learn Python programming

Make micro Python programs to learn Python programming
Make micro Python programs to learn Python programming

Hello everyone, In this post, we will learn Python by creating very simple programs.

1. Write a Python program that calculates the total bill amount, including tip, and splits it among a specified number of people.

Can you explain how each part of the program works?”

Let’s solve this program.

    print("Welcome to the tip calculator")
    bill = float(input("What was the total bill? Rs: "))
    tip = int(input("How much tip would you like to give? 10, 12, or 15?: "))
    people = int(input("How many people to split the bill?: "))


    tip_as_percent = tip / 100
    total_tip_amount = bill * tip_as_percent
    total_bill = bill + total_tip_amount
    bill_per_person = total_bill / people

    final_amount = round(bill_per_person, 2)
    final_amount = "{:.2f}".format(bill_per_person)
    print(f"Each person should pay: Rs {final_amount} and total bills is Rs: {total_bill}")

Let’s break down the code step by step:

  1. print("Welcome to the tip calculator"): This line prints a welcome message to the user, indicating that they are using a tip calculator.
  2. bill = float(input("What was the total bill? Rs: ")): This line prompts the user to input the total bill amount. The input() function takes user input as a string, which is then converted to a floating-point number using float() and assigned to the variable bill.
  3. tip = int(input("How much tip would you like to give? 10, 12, or 15?: ")): This line prompts the user to input the tip percentage they would like to give. It expects the user to input either 10, 12, or 15. The input is converted to an integer using int() and assigned to the variable tip.
  4. people = int(input("How many people to split the bill?: ")): This line prompts the user to input the number of people with whom they want to split the bill. The input is converted to an integer using int() and assigned to the variable people.
  5. tip_as_percent = tip / 100: This line calculates the tip percentage as a decimal. For example, if the user entered 15, tip_as_percent would be 0.15.
  6. total_tip_amount = bill * tip_as_percent: This line calculates the total tip amount by multiplying the bill amount (bill) by the tip percentage (tip_as_percent).
  7. total_bill = bill + total_tip_amount: This line calculates the total bill amount by adding the original bill amount (bill) to the total tip amount (total_tip_amount).
  8. bill_per_person = total_bill / people: This line calculates the amount each person needs to pay by dividing the total bill (total_bill) by the number of people (people).
  9. final_amount = round(bill_per_person, 2): This line rounds the amount each person needs to pay to two decimal places using the round() function.
  10. final_amount = "{:.2f}".format(bill_per_person): This line formats the rounded amount (bill_per_person) as a string with two decimal places using string formatting.
  11. print(f"Each person should pay: Rs {final_amount} and total bills is Rs: {total_bill}"): This line prints the final amount each person should pay, including the currency symbol “Rs”, the formatted amount (final_amount), and the total bill amount (total_bill).

Leave a Reply