Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Define Variable
#1
Hi,

I have been wondering for long that what's is the difference between the two definitions of "Tax =0"? One is "tax = 0" defined at the foremost; the second one is under the first if conditional statement. I am not sure whether definition under some specific conditional statement applies to the whole block.

Thanks a lot!

value = int(input("Value of gift: "))
 
if value < 5000:
    tax = 0
elif value <= 25000:
    tax = 100 + (value - 5000) * 0.08
elif value <= 55000:
    tax = 1700 + (value - 25000) * 0.10
elif value <= 200000:
    tax = 4700 + (value - 55000) * 0.12
elif value <= 1000000:
    tax = 22100 + (value - 200000) * 0.15
else:
    tax = 142100 + (value - 1000000) * 0.17
 
if tax == 0:
    print("No tax!")
else:
    print(f"Amount of tax: {tax} euros")
and

value = int(input("Value of gift: "))
 
tax = 0

if value < 5000:
    tax == 0
elif value <= 25000:
    tax = 100 + (value - 5000) * 0.08
elif value <= 55000:
    tax = 1700 + (value - 25000) * 0.10
elif value <= 200000:
    tax = 4700 + (value - 55000) * 0.12
elif value <= 1000000:
    tax = 22100 + (value - 200000) * 0.15
else:
    tax = 142100 + (value - 1000000) * 0.17
 
if tax == 0:
    print("No tax!")
else:
    print(f"Amount of tax: {tax} euros")
Reply
#2
(Nov-02-2022, 12:34 PM)xinyulon Wrote:
value = int(input("Value of gift: "))
  
tax = 0
 
if value < 5000:
    tax == 0
elif value <= 25000:
    tax = 100 + (value - 5000) * 0.08
elif value <= 55000:
    tax = 1700 + (value - 25000) * 0.10
elif value <= 200000:
    tax = 4700 + (value - 55000) * 0.12
elif value <= 1000000:
    tax = 22100 + (value - 200000) * 0.15
else:
    tax = 142100 + (value - 1000000) * 0.17
  
if tax == 0:
    print("No tax!")
else:
    print(f"Amount of tax: {tax} euros"))

In the second snippet, on line 6 you have comparison tax == 0, not assignment. Not that this line makes much sense. So if value < 5000 and no assignment on line 3 there will be problem. Overall - second one has flaws in the implementation
xinyulon likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
In the second piece of code, on line 6, you've got the expression tax == 0. It's evaluated and the result thrown away because you don't do anything with it. It's not a statement that has a side effect, like an assignment (e.g. tax = 0) is.
xinyulon likes this post
Reply
#4
This is an error:
if value < 5000:
    tax == 0
tax == 0 is doing a comparison, not an assignment. It is saying "Is tax equal to 0?" You don't do anything with the results of the comparison, so the statement has no effect on your program at all.

Unfortunately, it is an error that has no ill effects. Prior to the statement, tax is already assigned the value 0. Your error, doing a comparison instead of assigning tax = 0, has no effect on the program because tax is already zero. I say that is an unfortunate error because no visible side effect leads you to thinking the code is correct. In the future, when the tax code changes and all gifts are taxed, some newbie programmer will edit your program to set the minimum tax rate to 1% like this:
if value < 5000:
    tax == value * 0.01
Now the error has a side effect.

The error that is not an error points to a redundancy in you code. There is no need for this:
tax = 0
 
if value < 5000:
    tax = 0
You could do this:
f value < 5000:
    tax == 0
But I know some programmers like to assign variables a default value outside of logic. I prefer writing the code like this:
value = int(input("Value of gift: "))
  
tax = 0
if value > 1000000:
    tax = 142100 + (value - 1000000) * 0.17
elif value > 200000:
    tax = 22100 + (value - 200000) * 0.15
elif value > 55000:
    tax = 4700 + (value - 55000) * 0.12
elif value > 25000:
    tax = 1700 + (value - 25000) * 0.10
elif value > 5000:
    tax = 100 + (value - 5000) * 0.08

if tax == 0:
    print("No tax!")
else:
    print(f"Amount of tax: {tax} euros")
I think it reads better than your code. With your code I found myself having to read past the calculation and back up to get the tax. For example, if the value of the gift was 70,000, I had to read past the "if value <= 20000" to "if value < 100000" to find the range, then back up to get the tax calculation "ax = 22100 + (value - 200000) * 0.15'. Starting with higher values and working I find 70,000 > 20,000 and the tax calculation is on the next line.

Are you sure about your logic? It seems odd that the tax for a 5000 euro gift is 0 and the tax for a 5001 euro gift is 100.08. To me it looks like the minimum tax rate is 2%. The tax for a 5000 euro gift would be 100 euro which makes more sense when looking at your other tax calculations. Or should gifts valued less than 5000 euro not be tax, and the tax for a 5001 euro gift should be 0.08?
xinyulon likes this post
Reply
#5
(Nov-02-2022, 09:12 PM)deanhystad Wrote: This is an error:
if value < 5000:
    tax == 0
tax == 0 is doing a comparison, not an assignment. It is saying "Is tax equal to 0?" You don't do anything with the results of the comparison, so the statement has no effect on your program at all.

Unfortunately, it is an error that has no ill effects. Prior to the statement, tax is already assigned the value 0. Your error, doing a comparison instead of assigning tax = 0, has no effect on the program because tax is already zero. I say that is an unfortunate error because no visible side effect leads you to thinking the code is correct. In the future, when the tax code changes and all gifts are taxed, some newbie programmer will edit your program to set the minimum tax rate to 1% like this:
if value < 5000:
    tax == value * 0.01
Now the error has a side effect.

The error that is not an error points to a redundancy in you code. There is no need for this:
tax = 0
 
if value < 5000:
    tax = 0
You could do this:
f value < 5000:
    tax == 0
But I know some programmers like to assign variables a default value outside of logic. I prefer writing the code like this:
value = int(input("Value of gift: "))
  
tax = 0
if value > 1000000:
    tax = 142100 + (value - 1000000) * 0.17
elif value > 200000:
    tax = 22100 + (value - 200000) * 0.15
elif value > 55000:
    tax = 4700 + (value - 55000) * 0.12
elif value > 25000:
    tax = 1700 + (value - 25000) * 0.10
elif value > 5000:
    tax = 100 + (value - 5000) * 0.08

if tax == 0:
    print("No tax!")
else:
    print(f"Amount of tax: {tax} euros")
I think it reads better than your code. With your code I found myself having to read past the calculation and back up to get the tax. For example, if the value of the gift was 70,000, I had to read past the "if value <= 20000" to "if value < 100000" to find the range, then back up to get the tax calculation "ax = 22100 + (value - 200000) * 0.15'. Starting with higher values and working I find 70,000 > 20,000 and the tax calculation is on the next line.

Are you sure about your logic? It seems odd that the tax for a 5000 euro gift is 0 and the tax for a 5001 euro gift is 100.08. To me it looks like the minimum tax rate is 2%. The tax for a 5000 euro gift would be 100 euro which makes more sense when looking at your other tax calculations. Or should gifts valued less than 5000 euro not be taxt, and the tax for a 5001 euro gift shold be 0.08?

I would write this program without using an if statements.

Hi,

Thank you so much for your kind and detailed explanation. It is of great help and I appreciate it a lot! Yours indeed look way much better than mine!!! However, one thing confuses me is the code under "you could do this". Isn't that the same as what I have written?

I think I am more used to assign out of the logic so I can't think of other ways except for either doing a redundant statement tax = 0 under "if value < 5000" or "tax ==0" comparison.

Do correct me if I am wrong (since I am still a beginner), based on my understanding, assignments made out side of a logic have the same effect as assignments within a logic right? I have played around with my Python and it seems so?

Regarding the last point, yes I am sure the logic is correct since I am following a given exercise which is based on the real tax law of the country.
Reply
#6
Here's another, cleaner way to do this:
tax_table = [[5000, 0.08, 100], [25000, 0.10, 1700],
    [55000, 0.12, 4700],[200000, 0.15, 22100],[1000000, 0.17, 142100]]

def find_tax_amount(gift_amount):
    idx = 0
    limit = len(tax_table)

    while (idx < limit) and gift_amount > tax_table[idx][0]:
        idx += 1
    return idx -1

def main():
    value = int(input("Value of gift: $"))
    idx = find_tax_amount(value)
    if idx < 0:
        print("No tax!")
    else:
        tax = tax_table[idx][2] + ((value - tax_table[idx][0]) * tax_table[idx][1])
        print(f"tax due: ${tax}")

main()
test:
Output:
Value of gift: $3000 No tax! Value of gift: $7000 tax due: $260.0 Value of gift: $1000001 tax due: $142100.17
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use a variable in Python (2.x) to define decimal part? MDRI 4 2,344 May-07-2021, 12:39 AM
Last Post: MDRI
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,662 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas
  define a variable using an if statement Margaridalopes 2 2,190 Oct-24-2020, 05:47 PM
Last Post: jefsummers
  How to I define a variable between strings in telnetlib write? Fez 2 3,409 May-02-2019, 06:53 PM
Last Post: Fez

Forum Jump:

User Panel Messages

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