Default parameters: Calculate splitting a check between diners - python

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.

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

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

Related

Calculating Percentage Python

Im building a basic income tax calculator but can't figure out how to do all the necessary calculations
if income in range(120001, 180000):
tax = income + 29467
tax = income / 0.37
The given income if in this range needs to be $29,467 plus 37c for each $1 over $120,000 but i have no clue how to apply both calculations correctly
If I understood correctly, then try this option.
income = int(input('You income: '))
if income >= 120001 and income <= 180000:
tax = (29467 + (income - 120001) * 0.37)
print(f'income = {income} , tax = {tax} dollars')
Solution:
You income: 123500
income = 123500 , tax = 30761.63 dollars
why did you stated "tax" twice? then the first "tax" state will be wasted which means I shades with the latter "tax".
My translation of the question is this:
If income is in the range 120,000 to 180,000 then add a constant amount of 29,467 then an additional 0.37 for every unit over 120,000.
If that is the case then:
def calculate_tax(income):
return 29_467 + 0.37 * (income - 120_000) if 120_000 < income < 180_000 else 0
print(f'{calculate_tax(150_000):,.2f}')
Output:
40,567.00
You have income tax brackets.
0-120000. The rate is 29.467%
120001-180000 the rate is 37%. Based on your data
So for an income of 150000. The income tax is 120000*0.29467 + (150000-120000)*0.37

How to work out the percentage of an inputted number?

I've started using python a few days ago and am having difficulty in adding 20% (VAT) of the answer to 'Enter the total cost of the meal'. So, say the user inputted 80, how would I get it to add 20% of the inputted answer (80) to the total cost? Thank you in advance!
quotient = 1 / 5
percentage = quotient * 100
total_cost = float(input('Enter the total cost of the meal: '))
total_cost_vat=total_cost+percentage
print("Total cost of meal inclusive of VAT is",total_cost_vat)
number_of_people = float(input('Enter the number of people paying: '))
var1 = float(input(total_cost_vat / number_of_people))
You first create a variable quotient and you make it equal 1/5 or 0.2.
But now you times it by 100? Why? And then instead of multiplying total_cost_vat you add basically 20 to it. Why?
vat_percentage = 0.2 # In math 0.2 is 20%
subtotal_cost = float(input('Enter the total cost of the meal: '))
# Add 1 to the percentage to get total + vat
total_cost = subtotal_cost * (1 + vat_percentage)
If you know for sure that the tax is going to be 20% every time you calculate the VAT you can just multiply the total cost by 1.2, otherwise you can have the tax as a variable in decimal form. Then you multiply the pre tax cost by 1 + the tax.
tax = 0.20
pre_tax = float(input('Enter the total cost of the meal: '))
total_cost_vat = pre_tax * (1 + tax)
print("Total cost of meal inclusive of VAT is",total_cost_vat)
number_of_people = float(input('Enter the number of people paying: '))
print(total_cost_vat / number_of_people)
I coded it up below and annotated it. Some of the code you had wasn't necessary.
If you don't know that it is always going to be 20% you can use the below code and change the value of the variable:
# step 1: simpler than 1/5
percentage = 0.2
# step 2: getting input
total_cost = float(input('Enter the total cost of the meal: '))
# step 3: calculating the cost with the vat
total_cost_vat = total_cost + (percentage * total_cost)
# step 4: printing our value
print(total_cost_vat) # printing vat value
If you do know the value of the vat you can replace step 3 with below code:
total_cost_vat = 1.2 * total_cost
Hope this helped!

Calculate The Discount of a Product using a Python Code

I am currently trying to create a code that will calculate the discount (in percentage) of a product.
The code is supposed to ask the user of the code for the original price of the code, the discounted price, then it should tell the user what the discount was (in percentage). But it is not showing the correct percentage right now.
Here is the code so far:
o = float(input('The original ticket price: $'))
d = float(input('The price after the discount: $'))
p = 100/(o/d)
p2 = str(round(p, 2))
print('The discount (in percentage) is', p2,'%')
The calculations that you put in your code are incorrect.
Here's an improved version that I whipped up, and I hope this helps you:
if option == 3:
o = float(input('The ticket (original) price($): '))
dp = float(input('Price after the discount($): '))
p = 100/(o/dp)
d = str(round(100 - p, 2))
print('The percentage discount is', d,'%')
The formula you are using is not correct, try this:
o = float(input('The original ticket price: $'))
d = float(input('The price after the discount: $'))
p = (1 - d / o) * 100
p2 = str(round(p, 2))
print('The discount (in percentage) is', p2,'%')
Just did something similar to this myself. Here's the code I used:
It's essentially using the following formula to work out the amount saved, then deducts it from the main cost:
Total = cost * sunflower
Discount = Total * 0.1
FinalCost = Total - Discount
An actual functional piece of code would look like:
# $5.00 Sunflowers at 10% off calculator:
#variables
cost = 5
Sunflowers = 10
#calculator
Total = float(cost) * int(Sunflowers)
Discount = float(Total) * 0.1
FinalCost = float(Total) - float(Discount)
print("Final price of: ", Sunflowers , ": $", FinalCost)

Rounding number up using .format()

I’m writing a simple program and I’m using .format() to round the number to 2 d.p.
State = 0.05
County = 0.025
Purchase = float(input(‘amount of purchase: ‘))
State_tax = purchase * state
County_tax = purchase * county
Total_tax = state_tax + county_tax
Total = purchase + total_tax
Print(‘amount: ‘ + ‘{:.2f}’.format(purchase))
Print(‘state tax: ‘ + ‘{:.2f}.format(state_tax))
Print(‘county tax: ‘ + ‘{:.2f}.format(county_tax))
Print(‘total tax: ‘ + ‘{:.2f}.format(total_tax))
Print(‘total sale: ‘ + ‘{:.2f}.format(total))
To test it I inputted 11. However, the total doesn’t add up correctly to the tax. The total tax is 0.83 but the total is 11.82. It doesn’t round 11.825 to 11.83. How could I fix this?
Because you are calculating tax total without formatting
Try this one for tax as you shown in print
total_tax = float('{:.2f}'.format(state_tax)) + float('{:.2f}'.format(county_tax))
This fix fix your total
Python usually round to min value if fraction value is less than or equal to 5, It rounds up to max value when the fraction value is above 5.
You can even try this to round the number
print("amount: {}".format(round(purchase, 2)))
To round the amount to 2 digits

Why do I get into an infinite loop with this simple Python code?

balance = 4773
annualInterestRate = 0.2
pay = 10
while balance > 0:
for key in range(1,13):
balance -= pay
balance = balance + (balance * (annualInterestRate / 12.0))
pay += 10
print('Lowest Payment: '+str(pay))
When balance is 3329, it returns the good result of 310 but when 4773 I get into an infinite loop instead of getting the result of 440...
At each iteration of the while loop, balance increases, so it keeps greater than 0. You can print the intermediate values of balance:
balance = 4773
annualInterestRate = 0.2
pay = 10
while balance > 0:
print balance
for key in range(1,13):
balance -= pay
balance = balance + (balance * (annualInterestRate / 12.0))
pay += 10
Output:
4773
5686.32508646
6666.19699272
7727.21549777
8887.18344195
10167.8094501
11595.5648257
13202.7284403
15028.6608622
17121.3580173
...
Can you explain how you would obtain 440? With which calculus? So we can try to fix your code. It looks like 3390 is the greater (integer) initial value you can set to balance that converges.
In your loop, you do:
balance -= pay
balance = balance + (balance * (annualInterestRate / 12.0))
which is equivalent to:
balance = balance - pay + ((balance - pay) * (annualInterestRate / 12.0))
or:
balance = balance + balance * (annualInterestRate/12) - pay * (annualInterestRate/12)
Let's simplify but putting pay2 = pay * (annualInterestRate/12). So, basically, balance will decrease if pay2 is greater than the interest balance * (annualInterestRate/12).
In order to better understand what's going on, imagine it's a race between the interests and the increase of pay. The interests are greater than pay at the beginning so it keeps increasing but at some point, pay might be big enough to reduce the balance and at this point, pay will always be greater than the interest. However, if the interests start at a high level, the interest will grow and the pay will never reach the amount of interest (that's actually sad). The mathematical reason is that pay is linear whereas the interests are not and increase more and more as the balance continues growing.
Nothing's wrong witht the code. With your formula, the increase of balance at 4773 is more than the increase of payment, which is 10. That explains why the balance keeps growing to infinity, and thus the loop never ends.
while balance > 0:
for key in range(1,13):
balance -= pay
balance = balance + (balance * (annualInterestRate / 12.0))
pay += 10
I think you might want to lower your interest, or make your salary higher, so you can pay your debt :)

Categories