0
price = input("How much: ") 
country = input("which country are you from :")
tax = 0
total = int(price) + (int(price)*(tax/100)) 


if country =="Canada" : 

   province = input("Which province? :")
   if province == "Alberta" :
       tax = 5
       print(total)
   elif province == "Ontario" : 
       tax = 13
       print(total)
   else : 
       tax = 11
       print(total)
else : 
     tax = 0
     print(total)

This code does not update the tax and doesn't calculate total afterward accordingly. Can anyone suggest any solution?

5
  • 2
    You need to calculate the total after setting the tax value. Commented Jun 10, 2018 at 12:49
  • 3
    Possible duplicate of Updating a parameter's value doesn't update the result of the function? Commented Jun 10, 2018 at 12:51
  • Create a function for total like get_total(tax,price) etc that returns total value given tax and price in input and call that whenever you update tax. Commented Jun 10, 2018 at 12:53
  • Tbh, you need a good datastructure to save your data. Possibly a json-stucture (dictionary) Commented Jun 10, 2018 at 12:55
  • The code worked setting the tax value then calculating the total. Thanks guys for the help. Commented Jun 10, 2018 at 16:38

4 Answers 4

4

The issue is you are calculating total before changing tax to be the correct value. You can fix this by moving the calculation so that it happens after tax has been set.

price = input("How much: ")
country = input("which country are you from :")
tax = 0

if country == "Canada":
    province = input("Which province? :")
    if province == "Alberta":
        tax = 5
    elif province == "Ontario":
        tax = 13
    else:
        tax = 11
else:
    tax = 0

total = int(price) + (int(price)*(tax/100))
print(total)
Sign up to request clarification or add additional context in comments.

Comments

1

Well, the problem is, you pre-calculated the total before the if statement with tax = 0. That would always return the same value. Try calculating the total everytime you update the tax. Something like this:

tax = 5
total = int(price) + (int(price)*(tax/100)) 
print(total)

Comments

1

Have a look at this simple example. I think it can help you "rethink" your data-sctructure. Good luck!

taxes = {
    'Canada': {
        'Alberta': 5,
        'Ontario': 13,
        'default': 11
    }
}

def taxfunc(price, tax):
    return price + price*tax/100

price = int(input("How much: "))
country = input("which country are you from :").title()

if country in taxes:
    province = input("Which province? :").title()
    tax = taxfunc(price, taxes[country].get(province, taxes[country]['default']))
    print('Your tax is: {}'.format(tax))
else:
    print('no data')

1 Comment

Thanks for the help. I will try your method and get back to you.
0

Try this: After getting the tax, then calculate the total. Problem of your code is you calculate the total before assigning values to tax. Always your total is for tax = 0 only. But moving the total calculation to the bottom will fix the issue.

price = input("How much: ") 
country = input("which country are you from :")
tax = 0        

if country =="Canada" :     
   province = input("Which province? :")
   if province == "Alberta" :
       tax = 5
   elif province == "Ontario" : 
       tax = 13
   else : 
       tax = 11
else : 
   tax = 0

total = int(price) + (int(price)*(tax/100))
print(total)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.