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
Related
I want my output to have a 2 decimal places result. What should I do?
Ideal output is:
Enter the number of plants to purchase: 14
Enter the price per unit of plant: 250.00
The total payable amount is: P3,350.00
n1 = input('Enter number of plants: ')
n2= input('Enter price per unit: ')
mul= float(n1) * float(n2)
mul2= (float(n1)-float(10)) * (float(n2))
mul3 = float(10) * float(n2)
discount2 = float (mul2)* float(0.15)
total2=mul2-discount2
total3= total2+mul3
if float(n1)<=float(10):
mul= float(n1) * float(n2)
print('The total payable amount is: ', mul)
elif float(n1)>float(10):
print('The total payable amount is: ', total3)
A few answers missed the fact you also needed thousands separated by a comma. here is the format command which displays the price the way you expect it
n1 = input('Enter number of plants: ')
n2= input('Enter price per unit: ')
mul= float(n1) * float(n2)
mul2= (float(n1)-float(10)) * (float(n2))
mul3 = float(10) * float(n2)
discount2 = float (mul2)* float(0.15)
total2=mul2-discount2
total3= total2+mul3
if float(n1)<=float(10):
mul= float(n1) * float(n2)
print('The total payable amount is: ${:0,.2f}'.format(mul))
elif float(n1)>float(10):
print('The total payable amount is: ${:0,.2f}'.format(total3))
Use a formatted string. Also, convert the values to floats at the start, then there is no need to keep converting everything.
plants_str = input('Enter number of plants: ')
price_str = input('Enter price per unit: ')
plants = float(plants_str)
price = float(price_str)
total = plants * price
if plants > 10:
total -= 0.15 * (plants - 10) * price
print(f"The total payable amount is: {total:0,.2f}")
But as noted in a comment, for serious applications involving currency you should use the decimal library. This ensures decimal numbers are represented exactly.
from decimal import Decimal
plants_str = input('Enter number of plants: ')
price_str = input('Enter price per unit: ')
plants = Decimal(plants_str)
price = Decimal(price_str)
total = plants * price
if plants > 10:
total -= Decimal("0.15") * (plants - 10) * price
print(f"The total payable amount is: {total:0,.2f}")
You can use the str.format function and I made some small adjustments to your code.
n1 = float(input('Enter number of plants: '))
n2 = float(input('Enter price per unit: '))
mul = n1 * n2
if n1<=10:
print('The total payable amount is: ${:0,.2f}'.format(mul))
else:
print('The total payable amount is: ${:0,.2f}'.format(0.85*mul+1.5*n2))
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}")
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!
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)
One of my school assignments is to create a program where you can input a minimum number of passengers, a max number of passengers, and the price of a ticket. As the groups of passengers increase by 10, the price of the ticket lowers by 50 cents. At the end it is supposed to show the maximum profit you can make with the numbers input by the user, and the number of passengers and ticket price associated with the max profit. This all works, but when you input numbers that result in a negative profit, i get a run time error. What am i doing wrong? I have tried making an if statement if the profit goes below 0, but that doesn't seem to work. here is my work for you all to see. A lead in the right direction or constructive criticism would be a great help.
#first variables
passengers = 1
maxcapacity = 500
maxprofit = 0
ticketprice = 0
fixedcost = 2500
#inputs and outputs
again = 'y'
while (again == 'y'):
minpassengers = abs(int(input("Enter minimum number of passengers: ")))
maxpassengers = abs(int(input("Enter maximum number of passengers: ")))
ticketprice = abs(float(input("Enter the ticket price: ")))
if (minpassengers < passengers):
minpassengers = passengers
print ("You need at least 1 passenger. Setting minimum passengers to 1.")
if (maxpassengers > maxcapacity):
maxpassengers = maxcapacity
print ("You have exceeded the max capacity. Setting max passengers to 500.")
print ("Passenger Run from", minpassengers, "to", maxpassengers, "with an initital ticket price of $",format (ticketprice, "7,.2f"), "with a fixed cost of $2500.00\n"
"Discount per ticket of $0.50 for each group of 10 above the starting count of", minpassengers, "passengers")
for n in range (minpassengers, maxpassengers + 10, 10):
ticketcost = ticketprice - (((n - minpassengers)/10) * .5)
gross = n * ticketcost
profit = (n * ticketcost) - fixedcost
print ("Number of \nPassengers", "\t Ticket Price \t Gross \t\t Fixed Cost \t Profit")
print (" ", n, "\t\t$", format (ticketcost, "7,.2f"), "\t$", format (gross, "5,.2f"), "\t$", format(fixedcost, "5,.2f"), "\t$", format (profit, "5,.2f"))
if (profit > maxprofit):
maxprofit = profit
maxpass = n
best_ticket = ticketcost
print ("Your max profit is $", format (maxprofit, "5,.2f"))
print ("Your max profit ticket price is $", format (best_ticket,"5,.2f"))
print ("Your max profit number of passengers is", maxpass)
again = input ("Run this again? (Y or N): ")
again = again.lower()
print ("\n")
This is happening because the condition profit > maxprofit never evaluates to True in the situation where your profit is negative, because maxprofit is set to 0 up at the top. This in turn means that best_ticket never gets assigned a value in this case, and so Python can't print it out later.
You could avoid this problem either by setting a default value for best_ticket earlier in the program:
best_ticket = 0
or by adding a condition that you only print out a best ticket price when best_ticket is set:
# Earlier in the program.
best_ticket = None
if best_ticket is not None:
print("Your max profit ticket price is $", format(best_ticket,"5,.2f"))
Also, FYI, the same problem will occur for the maxpass variable.
Your error is telling you the problem
NameError: name 'best_ticket' is not defined
You define best_ticket in this block
if (profit > maxprofit):
maxprofit = profit
maxpass = n
best_ticket = ticketcost
Regardless of the truth of that statement, you reference best_ticket below
print ("Your max profit ticket price is $", format (best_ticket,"5,.2f"))
Your error is probably because the variable you are outputting is defined in a spot that doesn't get executed.
if (profit > maxprofit):
maxprofit = profit
maxpass = n
best_ticket = ticketcost
So, whenever the if condition is False, best_ticket never gets assigned.
Try adding best_ticket = 0 at the top of your code.
ticketprice = abs(float(input("Enter the ticket price: ")))
best_ticket = -1 #nonsense value that warns you whats happening.