Default Parameters: Calculate Splitting a Check Between Diners

Default Parameters: Calculate Splitting a Check Between Diners

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))
3

5 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.

10

Heres what I did:

  1. I named the parameters with all default amounts
  2. named new local variables to get new amounts
  3. 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

The paragraph outlines what your parameters needs to be; bill, people, tax percentage and tip percentage. it also tells you that you need a tip of 15% and a tax rate of 9%. so from there you can write your function.

def split_check(bill, people, tax = 0.09, tip = 0.15):

"bill" and "people" will be a user input so they do not get a value. but now you have to define the calculations for the tax, the tip and the split. tax will be bill * tax and tip will be tip * bill, then the total will be bill + tax + tip / people

def split_check(bill, people, tax = 0.09, tip = 0.15):
    tax = bill * tax
    tip = bill * tip
    return(bill + tax + tip) / people

do not add hard values to the areas that say float(input()), int(input()) or anywhere in your programing that requires an input. These are for user input.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.