Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Total after tax
#1
Hello everyone! :)

Can someone please explain to me what is wrong with my code? I need to give the total of the amount a user enters and show how much it is after 6% tax is added, but my program is not working correctly. Say for instance I enter the number 10, the result is 20. Thanks in advance for any help!
#!/usr/bin/env python3

tax = 0.06

def sales_tax(total):
    sales_tax = total * tax
    return total

def main():
    print("Sales Tax Calculator\n")
    total = float(input("Enter total: "))
    total_after_tax = round(total + sales_tax(total), 2)
    print("Total after tax: ", total_after_tax)
    
if __name__ == "__main__":
    main()
Reply
#2
Look at your sales_tax() function. You calculate one value, but that's not what you return. So later, when you calculate the total after tax, you never actually add tax, you just add the total to itself.
Reply
#3
(Apr-26-2017, 05:32 AM)nilamo Wrote: Look at your sales_tax() function.  You calculate one value, but that's not what you return.  So later, when you calculate the total after tax, you never actually add tax, you just add the total to itself.

Ah ok, I get anxious when I do code, so I overcomplicate things. Can you give me a tip on how to fix that? :/

Nvm, I figured it out. :) I kept reading what you said and realised my mistake, lol. Thanks.
Reply
#4
Try this...

# GLOBAL CONSTANT
TAX_RATE = 0.06
 
def main():
  
    print("Sales Tax Calculator\n")
    total = float(input("Enter total: $"))
    sales_tax = (total * TAX_RATE) + total
    print("Total after tax: $", format(sales_tax, '.2f'))
    
main()
Reply
#5
(Apr-27-2017, 04:26 AM)PyPhanman Wrote: Try this...
Note that your code is not related to the OP specific question, which has been answered more than a day ago. Also no need to repeat already given answer.
The fact that if __name__ ==  'main': block is not present in your code removes the need to def main function and calling it afterwards.
It is misleading to use name sales_tax when actually it's after-tax amount (i.e. pre-tax amount + tax amount)
Also, given that we are in Homework section, removing OP sales_tax function may be against his/her homework assignment requirements.
Reply
#6
(Apr-27-2017, 06:37 AM)buran Wrote:
(Apr-27-2017, 04:26 AM)PyPhanman Wrote: Try this...
Note that your code is not related to the OP specific question, which has been answered more than a day ago. Also no need to repeat already given answer.
The fact that if __name__ ==  'main': block is not present in your code removes the need to def main function and calling it afterwards.
It is misleading to use name sales_tax when actually it's after-tax amount (i.e. pre-tax amount + tax amount)
Also, given that we are in Homework section, removing OP sales_tax function may be against his/her homework assignment requirements.

Although calling it an after-tax amount is misleading as well, since it usually means money received after deducting tax.  Cool
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020