0% found this document useful (0 votes)
10 views2 pages

2

Uploaded by

Live Channel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

2

Uploaded by

Live Channel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

class BillSplitter:

def __init__(self):
self.history = []

def add_bill(self):
description = input("Enter description for the bill: ").strip()
currency = input("Enter currency (e.g., Rs., $, €): ").strip()

# Bill amount
while True:
try:
bill = float(input(f"Total bill? {currency}"))
if bill <= 0: raise ValueError
break
except ValueError:
print("Enter a valid bill amount.")

# Tip percentage
while True:
try:
tip = int(input("Tip percentage? "))
if tip < 0: raise ValueError
break
except ValueError:
print("Enter a valid tip percentage.")

# Number of people
while True:
try:
people = int(input("How many people? "))
if people <= 0: raise ValueError
break
except ValueError:
print("Enter a valid number of people.")

# Extra individual expenses


individual_expenses = {}
if input("Any extra expenses? (yes/no): ").strip().lower() == "yes":
for _ in range(people):
name = input("Name: ").strip()
while True:
try:
expense = float(input(f"Extra expense for {name}:
{currency}"))
if expense < 0: raise ValueError
individual_expenses[name] = expense
break
except ValueError:
print("Enter a valid expense.")

# Calculate total bill and per person cost


tip_amount = bill * (tip / 100)
total_bill = bill + tip_amount
per_person = total_bill / people

# Save the bill history


self.history.append({
"description": description, "currency": currency, "bill": bill,
"tip": tip, "total_bill": total_bill, "people": people,
"individual_expenses": individual_expenses, "bill_per_person":
round(per_person, 2)
})

# Show bill summary


print(f"\n--- Bill Summary ---")
print(f"Description: {description}")
print(f"Total: {currency}{bill:.2f}, Tip: {currency}{tip_amount:.2f}, Total
with tip: {currency}{total_bill:.2f}")
print(f"Each pays: {currency}{round(per_person, 2)}")
for name, expense in individual_expenses.items():
print(f"{name}: {currency}{expense:.2f}")

def show_history(self):
if not self.history:
print("No bills recorded yet.")
return
for i, record in enumerate(self.history, 1):
print(f"\nBill {i}: {record['description']}")
print(f"Total: {record['currency']}{record['bill']:.2f}, Tip:
{record['currency']}{record['total_bill']:.2f}")
print(f"Each pays: {record['currency']}
{record['bill_per_person']:.2f}")
for name, expense in record["individual_expenses"].items():
print(f"{name}: {record['currency']}{expense:.2f}")

def run(self):
while True:
print("\n--- Menu ---")
print("1. Add bill")
print("2. View history")
print("3. Exit")
choice = input("Choice (1/2/3): ").strip()
if choice == "1": self.add_bill()
elif choice == "2": self.show_history()
elif choice == "3": break
else: print("Invalid choice.")

# Run the program


if __name__ == "__main__":
BillSplitter().run()

You might also like