Posts: 53
Threads: 14
Joined: Mar 2017
Hey guys this is my first attempt at this program and I am having trouble with the second task which requires me to write a function that returns the amount of tax – it should include two parameters, one for the tax rate and another for the amount.
Also, using a for loop, call the function to calculate the tax on the user specified amount for tax rates of 5%, 10%, 15%, 20% and 25%:
So far I have managed to figure out how to write a basic code that takes the price of an item them returns the tax by a specific amount but I am struggling on how to return it in a list similar to this
5% tax on a Chocolate cake costing $17.50 is $0.88
10% tax on a Chocolate cake costing $17.50 is $1.75
15% tax on a Chocolate cake costing $17.50 is $2.62
20% tax on a Chocolate cake costing $17.50 is $3.50
25% tax on a Chocolate cake costing $17.50 is $4.38
Here is my code and output for the first part of my program
Any help is really appreciated :)
My code
print ("This is a tax calculator. Enter in the price and your item\n"
"and the program will calculate how much it costs with tax.""")
#Create tax function that takes price and tax_rate as imputs and returns the total price
def calculate_tax(price, tax_rate):
total = price + (price * tax_rate)
total = round(total, 2)
return total
#float is a variable type for decimals
item = str(input("Please enter an item"))
my_price = float(input("Price of the item $"))
tax = calculate_tax(my_price,.0875)
print ("The price of",item,"with tax: $",tax,"") Aaaand my output is something like this:
This is a tax calculator. Enter in the price and your item
and the program will calculate how much it costs with tax.
Please enter an itemChocolate Cake
Price of the item $17.50
The price of Chocolate Cake with tax:$ 19.03
Process finished with exit code 0
Posts: 2,953
Threads: 48
Joined: Sep 2016
You can print all in one for loop.
>>> for tax in range(5, 26, 5):
... print(tax)
5
10
15
20
25
Posts: 12,030
Threads: 485
Joined: Sep 2016
print ("This is a tax calculator. Enter in the price and your item\n"
"and the program will calculate how much it costs with tax.""")
#Create tax function that takes price and tax_rate as imputs and returns the total price
def calculate_tax(price, start, end, increment):
for n in range(start, end, increment):
tax_rate = n * .01
tax = price * tax_rate
total = round((price + tax), 2)
yield n, tax, tax_rate, total
#float is a variable type for decimals
item = str(input("Please enter an item: "))
my_price = float(input("Price of the item $"))
for n, tax, tax_rate, total in calculate_tax(my_price, 5, 26, 5):
print('{}% tax on a {} costing ${:.2f} is ${:.2f} for a total of ${:.2f}'
.format(n, item, my_price, tax, total)) test:
Output: This is a tax calculator. Enter in the price and your item
and the program will calculate how much it costs with tax.
Please enter an item: Chocolate Cake
Price of the item $17.5
5% tax on a Chocolate Cake costing $17.50 is $0.88 for a total of $18.38
10% tax on a Chocolate Cake costing $17.50 is $1.75 for a total of $19.25
15% tax on a Chocolate Cake costing $17.50 is $2.62 for a total of $20.12
20% tax on a Chocolate Cake costing $17.50 is $3.50 for a total of $21.00
25% tax on a Chocolate Cake costing $17.50 is $4.38 for a total of $21.88
Posts: 53
Threads: 14
Joined: Mar 2017
Mar-25-2017, 10:10 AM
(This post was last modified: Mar-25-2017, 10:24 AM by Liquid_Ocelot.)
Ok I managed to get it to print out the in the correct format but I am having trouble with printing the actual equation
So if the percentage is the user_input number * each number in taxlist / 100
I just need it to print out like this
5% tax on a Chocolate cake costing $17.50 is $0.88
10% tax on a Chocolate cake costing $17.50 is $1.75
15% tax on a Chocolate cake costing $17.50 is $2.62
20% tax on a Chocolate cake costing $17.50 is $3.50
25% tax on a Chocolate cake costing $17.50 is $4.38
And my code is now looking like this so I am just confused how to put that equation in there to get it to print out the tax result at the end for each number in the list
item = str(input("Please enter an item"))
price = float(input("Price of the item $"))
taxlist = [5,10,15,20,25]
def result (price, tax2):
total = price * taxlist[0,1,2,3,4,4] / 100
return total
for tax in range(5, 26, 5):
print("",tax,"% tax on a ",item," costing ",price," is",result,"") my output gives me a strange result too so It may just be something simple I am missing I am not sure lol
Please enter an itemCake
Price of the item $17.50
5 % tax on a Cake costing 17.5 is <function result at 0x000001B4EB9201E0>
10 % tax on a Cake costing 17.5 is <function result at 0x000001B4EB9201E0>
15 % tax on a Cake costing 17.5 is <function result at 0x000001B4EB9201E0>
20 % tax on a Cake costing 17.5 is <function result at 0x000001B4EB9201E0>
25 % tax on a Cake costing 17.5 is <function result at 0x000001B4EB9201E0>
Process finished with exit code 0 Also can someone explain why my output of the price of the item drops off the zero at the end, It should be 17.50 not 17.5
Thanks
Moderator Larz60+: Fixed tags you had wrong '\' should be '/'
No worries this has been understood and solved thanks heaps dude
Posts: 2,953
Threads: 48
Joined: Sep 2016
Mar-25-2017, 10:33 AM
(This post was last modified: Mar-25-2017, 10:40 AM by wavic.)
In [10]: def result(item, price):
...: for tax in range(5, 26, 5):
...: percentage = price * tax / 100
...: print("{:>2}% tax on a {} costing ${:.2f} is ${:.2f}".format(tax, item, price, percentage))
...:
In [11]: result(item, price)
5% tax on a Cake costing $17.50 is $0.88
10% tax on a Cake costing $17.50 is $1.75
15% tax on a Cake costing $17.50 is $2.62
20% tax on a Cake costing $17.50 is $3.50
25% tax on a Cake costing $17.50 is $4.38
My prev. post was a hint how to generate the tax list and use each tax to calculate the percentage
Posts: 53
Threads: 14
Joined: Mar 2017
Mar-25-2017, 10:33 AM
(This post was last modified: Mar-25-2017, 10:41 AM by Liquid_Ocelot.)
Larz+60 Just wondering how you would go about making it a return function instead of yield as my instructions don't mention anything about yield and I do not think we are up to that keyword yet
return n, tax, tax_rate, total I tried changing yield into return but I get this error...
for n, tax, tax_rate, total in calculate_tax(my_price, 5, 26, 5):
TypeError: 'int' object is not iterable
(Mar-25-2017, 10:33 AM)wavic Wrote: In [10]: def result(item, price): ...: for tax in range(5, 26, 5): ...: percentage = price * tax / 100 ...: print("{:>2}% tax on a {} costing ${:.2f} is ${:.2f}".format(tax, item, price, percentage)) ...: In [11]: result(item, price) 5% tax on a Cake costing $17.50 is $0.88 10% tax on a Cake costing $17.50 is $1.75 15% tax on a Cake costing $17.50 is $2.62 20% tax on a Cake costing $17.50 is $3.50 25% tax on a Cake costing $17.50 is $4.38 My prev. post was a hint how to generate the tax list and use each tax to calculate the percentage
Awesome Thanks I have got it now :)
Posts: 2,953
Threads: 48
Joined: Sep 2016
Look at @ Larz60+ code. He uses more general approach. Even if don't use it because of the yield statement.
Posts: 12,030
Threads: 485
Joined: Sep 2016
without generator:
print ("This is a tax calculator. Enter in the price and your item\n"
"and the program will calculate how much it costs with tax.")
# Create tax function that takes price and tax_rate as inputs and returns the total price
# def calculate_tax(price, start, end, increment):
# for n in range(start, end, increment):
# tax_rate = n * .01
# tax = price * tax_rate
# total = round((price + tax), 2)
# yield n, tax, tax_rate, total
def calculate_tax(price, tax_rate):
return (price * tax_rate) * .01
# float is a variable type for decimals
item = str(input("Please enter an item: "))
my_price = float(input("Price of the item $"))
for rate in [5, 10, 15, 20, 25]:
tax = calculate_tax(my_price, rate)
print('{}% tax on a {} costing ${:.2f} is ${:.2f} for a total of ${:.2f}'
.format(rate, item, my_price, tax, my_price + tax)) test:
Output: This is a tax calculator. Enter in the price and your item
and the program will calculate how much it costs with tax.
Please enter an item: choc
Price of the item $17.5
5% tax on a choc costing $17.50 is $0.88 for a total of $18.38
10% tax on a choc costing $17.50 is $1.75 for a total of $19.25
15% tax on a choc costing $17.50 is $2.62 for a total of $20.12
20% tax on a choc costing $17.50 is $3.50 for a total of $21.00
25% tax on a choc costing $17.50 is $4.38 for a total of $21.88
|