Default parameters: Calculate splitting a check between diners
Question:
Can anyone help me writing the split_check()
function?.
The problem statement is:
Write a split_check function that returns the amount that each diner
must pay to cover the cost of the meal. The function has 4 parameters:
bill: The amount of the bill. people: The number of diners to split
the bill between. tax_percentage: The extra tax percentage to add to
the bill. tip_percentage: The extra tip percentage to add to the bill.
The tax or tip percentages are optional and may not be given when
calling split_check. Use default parameter values of 0.15 (15%) for
tip_percentage, and 0.09 (9%) for tax_percentage
I need to calculate the amount of tip and tax, add to the bill total, then divide by the number of diners.
bill = float(input())
people = int(input())
#Cost per diner at the default tax and tip percentages
print('Cost per diner:', split_check(bill, people))
bill = float(input())
people = int(input())
new_tax_percentage = float(input())
new_tip_percentage = float(input())
# Cost per diner at different tax and tip percentages
print('Cost per diner:', split_check(bill, people, new_tax_percentage, new_tip_percentage))
Answers:
You can see only bill
and people
are required, so you should add default values to your arguments:
def split_check(bill, people, tax = 0.09, tip = 0.15)
That means that if only two arguments are given, like in the first case, the tax and tip percentages will be 9% and 15% respectively. You should add to bill
the amount bill*tax
and bill*tip
. In the end, you’ll divide the bill by the number of people and return it.
So we have this:
def split_check(bill, people, tax = 0.09, tip = 0.15):
taxes = bill * tax
tips = bill * tip
return (bill + taxes + tips) / people
You can also check if people
is not smaller or equal to 0, if the arguments are numbers and not strings for example, and if tax
and tip
are between 0 and 1, etc. using a try
/except
block.
Taking all of the comments into account to understand the problem completely, here’s an answer that produces the right result:
def split_check(bill, people, tax = 0.15, tip = 0.09):
tax = bill * tax
tip = bill * tip
return (bill + tax + tip)/people
print('Cost per diner:', split_check(25, 2))
Or more concisely:
def split_check(bill, people, tax = 0.15, tip = 0.09):
return bill * (1.0 + tax + tip) / people
print('Cost per diner:', split_check(25, 2))
Result:
Cost per diner: 15.5
Heres what I did:
- I named the parameters with all default amounts
- named new local variables to get new amounts
- returned the per_person calculation
def split_check(bill=0, people=0, tax_percentage=0.09, tip_percentage=0.15):
new_check = (bill * tax_percentage) + bill
new_tip = bill * tip_percentage
per_person = (new_check + new_tip) / people
return per_person
bill = float(input())
people = int(input())
# Cost per diner at the default tax and tip percentages
print('Cost per diner:', split_check(bill, people))
bill = float(input())
people = int(input())
new_tax_percentage = float(input())
new_tip_percentage = float(input())
# Cost per diner at different tax and tip percentages
print('Cost per diner:', split_check(bill, people, new_tax_percentage, new_tip_percentage))
this works for me
def split_check(bill=0.0, people=0, tax_percentage=0.09, tip_percentage=0.15):
bill_per_diner = ((bill + ((tax_percentage * bill) + (tip_percentage * bill))) / people)
return bill_per_diner
Can anyone help me writing the split_check()
function?.
The problem statement is:
Write a split_check function that returns the amount that each diner
must pay to cover the cost of the meal. The function has 4 parameters:
bill: The amount of the bill. people: The number of diners to split
the bill between. tax_percentage: The extra tax percentage to add to
the bill. tip_percentage: The extra tip percentage to add to the bill.
The tax or tip percentages are optional and may not be given when
calling split_check. Use default parameter values of 0.15 (15%) for
tip_percentage, and 0.09 (9%) for tax_percentage
I need to calculate the amount of tip and tax, add to the bill total, then divide by the number of diners.
bill = float(input())
people = int(input())
#Cost per diner at the default tax and tip percentages
print('Cost per diner:', split_check(bill, people))
bill = float(input())
people = int(input())
new_tax_percentage = float(input())
new_tip_percentage = float(input())
# Cost per diner at different tax and tip percentages
print('Cost per diner:', split_check(bill, people, new_tax_percentage, new_tip_percentage))
You can see only bill
and people
are required, so you should add default values to your arguments:
def split_check(bill, people, tax = 0.09, tip = 0.15)
That means that if only two arguments are given, like in the first case, the tax and tip percentages will be 9% and 15% respectively. You should add to bill
the amount bill*tax
and bill*tip
. In the end, you’ll divide the bill by the number of people and return it.
So we have this:
def split_check(bill, people, tax = 0.09, tip = 0.15):
taxes = bill * tax
tips = bill * tip
return (bill + taxes + tips) / people
You can also check if people
is not smaller or equal to 0, if the arguments are numbers and not strings for example, and if tax
and tip
are between 0 and 1, etc. using a try
/except
block.
Taking all of the comments into account to understand the problem completely, here’s an answer that produces the right result:
def split_check(bill, people, tax = 0.15, tip = 0.09):
tax = bill * tax
tip = bill * tip
return (bill + tax + tip)/people
print('Cost per diner:', split_check(25, 2))
Or more concisely:
def split_check(bill, people, tax = 0.15, tip = 0.09):
return bill * (1.0 + tax + tip) / people
print('Cost per diner:', split_check(25, 2))
Result:
Cost per diner: 15.5
Heres what I did:
- I named the parameters with all default amounts
- named new local variables to get new amounts
- returned the per_person calculation
def split_check(bill=0, people=0, tax_percentage=0.09, tip_percentage=0.15):
new_check = (bill * tax_percentage) + bill
new_tip = bill * tip_percentage
per_person = (new_check + new_tip) / people
return per_person
bill = float(input())
people = int(input())
# Cost per diner at the default tax and tip percentages
print('Cost per diner:', split_check(bill, people))
bill = float(input())
people = int(input())
new_tax_percentage = float(input())
new_tip_percentage = float(input())
# Cost per diner at different tax and tip percentages
print('Cost per diner:', split_check(bill, people, new_tax_percentage, new_tip_percentage))
this works for me
def split_check(bill=0.0, people=0, tax_percentage=0.09, tip_percentage=0.15):
bill_per_diner = ((bill + ((tax_percentage * bill) + (tip_percentage * bill))) / people)
return bill_per_diner