Python assign variable names to if else function - python

# 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='')

Related

How to use format()?

My teacher gave me this assigment"Write a program that calculates the bill of sale for three items purchased. The cost of each item will be read from the user and stored into variables. The receipt will show the number of items purchased, the cost of each item, the average cost per item, the tax rate (.0825% in Texas), the tax amount, and the total cost" I'was able to do it with basic input and variables but i had some trouble when he asked me to use format() in the code because there's always a different error. Here's my attempt.
cost_1 = float(input("Enter cost of item 1:"))
cost_2 = float(input("Enter cost of item 2:"))
cost_3 = float(input("Enter cost of item 3:"))
cost = ['cost_1', 'cost_2', 'cost_3']
sentence = 'Cost 1: {0} Cost 2: {1} Cost 3: {2}'.format(cost, float['cost_1'], cost, float['cost_2'], cost, float['cost_3'])
print(sentence)
Average_cost = float(cost_1 + cost_2 + cost_3)/3
Total_cost = float(cost_1 + cost_2 + cost_3)
print("Average cost:", Average_cost)
print("Total cost:", Total_cost)
Tax_rate = float(8.25)
print("Tax rate:", Tax_rate)
Taxes = float(Total_cost*Tax_rate/100)
print ("Taxes:",float(Taxes))
Total = Total_cost + Taxes
print("Total:", Total)
I will not give you the answer, but I will explain a couple things you are doing wrong.
cost_1 = float(input("Enter cost of item 1:"))
cost_2 = float(input("Enter cost of item 2:"))
cost_3 = float(input("Enter cost of item 3:"))
# below you are assigning strings to the cost array, you should not have quotes here
cost = ['cost_1', 'cost_2', 'cost_3']
# below you are trying to format with three variables but are passing four.
# you are also trying to access the float function as a dict type.
sentence = 'Cost 1: {0} Cost 2: {1} Cost 3: {2}'.format(cost, float['cost_1'],
cost, float['cost_2'], cost, float['cost_3'])
print(sentence)
# there is no need to constantly float variables that are float
Average_cost = float(cost_1 + cost_2 + cost_3)/3
# agagin
Total_cost = float(cost_1 + cost_2 + cost_3)
print("Average cost:", Average_cost)
print("Total cost:", Total_cost)
# a float number is already a float, you dont need to declare it as float
Tax_rate = float(8.25)
print("Tax rate:", Tax_rate)
Taxes = float(Total_cost*Tax_rate/100)
print ("Taxes:",float(Taxes))
Total = Total_cost + Taxes
print("Total:", Total)
I will give you a little help with the cost list.
items_count = 3
cost = []
for i in range(items_count):
cost.append(float(input("enter price for item %d: " % (i+1))))
Also, there is a function called sum that will add up all the numbers in a list. Here is a tutorial https://www.programiz.com/python-programming/methods/built-in/sum

Sales tax calculator with a while loop

I am making a sales tax calculator and everything works besides the total payment amount portion. In my program I want to be able to input a number and get the taxed amount on the item I also want to be able to get the total amount payed including tax in the transaction. The code I have written so far does all of that but when I input a second number I want that numbers total payment to be added to the previous total payment. for example if you input 3.99 it gives you a total payment of 4.3441125000000005 then if you input 12.95 it should give you a total payment of 18.443425 because 12.95's total payment is 14.0993125 + 4.3441125000000005.
rate = 0.08875 #sales tax
amount = 1
while amount != 0:
amount = float(input("Enter item cost in dollars, 0 to quit: ")) #cost of item
tax = amount*rate #tax amount on item
total = amount+tax #total payment including tax
print("Tax amount on this item ==", tax)
print("Total payment ==", total)
so I basically just want the totals to add each time you input a cost.
You could create a new variable:
rate = 0.08875 #sales tax
amount = 1
full_total = 0
while amount != 0:
amount = float(input("Enter item cost in dollars, 0 to quit: ")) #cost of item
tax = amount*rate #tax amount on item
total = amount+tax #total payment including tax
full_total += total
print("Tax amount on this item ==", tax)
print("Total payment ==", total)
print(f"Your full total today is {full_total}")

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)

Python tax calculator - algorithm is not working correctly

I'm very new to python so I apologize if this question is simple. I am trying to write an algorithm that calculates the difference in rates between 2017 and 2018 based on a user-inputted salary. I've gotten to the point where the algorithm does calculate a tax rate, however it seems to do it backwards, i.e. the lower the inputted income, the higher the tax owed, something that the government, for all its flaws, generally doesn't do. I've tried different things for the algorithm but I'm still not sure where I'm going wrong. Any ideas or advice would be greatly appreciated.
Thanks!
# of tax brackets
levels = 6
#2017 tax rates
rates2017 = [0, 10, 15, 25, 28, 33, 35]
#2018 tax rates
rates2018 = []
#2017 income tax thresholds
incomes2017 = [0, 9325, 37950, 91900, 191650, 416700, 418400]
# take in a value for net income and assign it to int
netincome = int(input('Please input an integer for income: '))
#initialize the variables used
tax_owed = 0
taxable_income = 0
netincomeleft = netincome - 6500
i = levels
#while loop calculates the income tax
while i >= 0:
taxable_income = netincomeleft - incomes2017[i]
tax_owed += taxable_income * (rates2017[i]/100)
netincomeleft = incomes2017[i]
i -= 1
#multiply tax owed by -1 to get a positive int for clarity
taxes_owed = tax_owed * -1
# print out the 2017 tax owed
print('tax owed on $', netincome, 'after standard deduction is ', taxes_owed)
*for the sake of clarity, I'm using Python 3 in a Jupyter notebook environment
You are working mostly with negative numbers....you don't check whether their income EXCEEDS the specific level, so with an income of 100 you charge them NEGATIVE tax rate of (418400 - 100) and so on.
You want to start your level at the first number EXCEEDING netincomeleft, and then not multiply by -1!
So for a small income "level" should start at 0 or 1, not at 6.
Edit: I did figure this out eventually. Turns out the government does not collect more tax than income, except for special people. Thanks everyone for your help!
levels = 6
rates2017 = [0, 10, 15, 25, 28, 33, 35]
rates2018 = []
incomes2017 = [0, 9325, 37950, 91900, 191650, 416700, 418400]
netincome = int(input('Please input an integer for income: '))
tax_owed = 0
taxable_income = 0
standard_deduction = 6500
netincomeleft = netincome - standard_deduction
i = 0
while levels >= 0 and taxable_income >=0 and netincomeleft >= 0:
if (netincomeleft - incomes2017[i]) < 0:
taxable_income = netincome - incomes2017[i-1] - standard_deduction
else:
taxable_income = netincomeleft - incomes2017[i]
tax_owed += (taxable_income * (rates2017[i]/100))
netincomeleft = netincomeleft - incomes2017[i]
i += 1
levels -= 1
taxes_owed = tax_owed
print('tax owed on $', netincome, 'after standard deduction is ', taxes_owed)
def USA():
tax=0
if salary<=9700:
tax=9700*0.1
elif salary<=39475:
tax=970+(salary-9700)*0.12
elif salary<=84200:
tax=4543+(salary-39475)*0.22
elif salary<=160725:
tax=14382.5+(salary-84200)*0.24
elif salary<=204100:
tax=32748.5+(salary-160725)*0.32
elif salary<=510300:
tax=139918.5+(salary-204100)*0.35
else:
tax=328729.87+(salary-510300)*0.37
return ('tax owed on $', salary, 'after standard deduction is ', tax, 'and your netincome is ', (salary-tax) )
salary=int(input('Please input an integer for income: '))
print(USA())

Categories