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
Related
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
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!
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
# Determine price per pound
if quantity >= 40:
print('Cost of coffee $', format(quantity * 7.50,'.2f'),sep='')
else:
if quantity >= 20:
print ('Cost of coffee $', format(quantity * 8.75, '.2f'), sep='')
else:
if quantity >= 10:
print ('Cost of coffee $', format (quantity * 10.00, '.2f'), sep='')
else:
if quantity >= 1 or quantity <= 9:
print ('Cost of coffee $', format (quantity * 12.00, '.2f'), sep='')
Im trying to figure out how I get the total (cost per pound * quantity entered) assigned to a variable. I need to be able to take the total before tax and multiple it by 7% tax. Above is the formula I have to find out how much it will cost based on the quantity and price.
So you'll need to use another variable to keep track of the total cost, for this I'll use total. Then we can set that equal to quantity * price using the flow controls. For this you'll want to look further into using if, elif, else.
After that you can use the total to calculate the tax, which is pretty straight forward.
Finally you can use the same print statement you had in each if statement to output the total cost.
# initialize a variable to keep track of the total
total = 0
# Determine price per pound
# use if elif
if quantity >= 40:
total = quantity * 7.50
elif quantity >= 20:
total = quantity * 8.75
elif quantity >= 10:
total = quantity * 10.00
else:
total = quantity * 12.00
# do the tax calculations using total
total = total * 1.07
# print the result
print('Cost of coffee $', format(total,'.2f'), sep='')
If you want to calculate and use the tax separately, you'll need to use another variable. Just like the example above used total. This time we'll add a variable called tax.
Then we'll add another print statement to output it.
# initialize a variable to keep track of the total
total = 0
# Determine price per pound
# use if elif
if quantity >= 40:
total = quantity * 7.50
elif quantity >= 20:
total = quantity * 8.75
elif quantity >= 10:
total = quantity * 10.00
else:
total = quantity * 12.00
# do the tax calculations assigning it to a different variable
tax = total * 0.07
# add the tax to the total
total = total + tax
# print the tax
print('Tax $', format(tax,'.2f'), sep='')
# print the total
print('Cost of coffee $', format(total,'.2f'), sep='')
I am brand new to python, only a week or two into my course. Here is what I have written:
#prompt user for input
purchaseAmount = eval(input("please enter Purchase Amount: "))
# compute sales tax
salesTax = purchaseAmount * 0.06
# display sales tax to two decimal points
print("sales tax is", int(salesTax * 100 / 100.0))
and this is what it returns:
please enter Purchase Amount: 197.55
sales tax is 11
My text suggest I should get the answer 11.85.
What should I change to get 2 decimal places in my answer?
You should use the string formatting mini-language.
>>> salesTax = 22.2
>>> print("sales tax is %0.2f" % salesTax)
sales tax is 22.20
Alternatively use the newer format method
>>> salesTax = 22.256
>>> print("sales tax is {:.2f}".format(salesTax))
sales tax is 22.26
Use float instead:
print("sales tax is", round(float(salesTax * 100 / 100.0),2))
This will print the value to 2 decimal places as 11.85
You're taking int of the whole thing, which will always be an integer. You need to first take the int and then divide the result of the int by 100. So instead of this:
int(salesTax * 100 / 100.0)
Do this:
int(salesTax * 100) / 100.0
Its a minor correction. You could use float to get the exact answer with the decimal point. You will lose precision with int.
purchaseAmount = eval(input("please enter Purchase Amount: "))
salesTax = purchaseAmount * 0.06
# get salesTax in floating point number
salesTax = float(salesTax * 100 / 100.0)
print salesTax
-> 11.853