-2

Im in a beinning programming class and i have a tax program to make in pything, i have it working but for some reason, the If else statement is triggering the calculations after the if when it should end when just printing. Here is my code

taxr = 0.2#Sets the default tax rate
stdded = 10000 # sets the standard deduction
depded = 2000 # sets the dependent deduction
income = int(input("Enter your income: ")) # User input's their income
if income<20000: print('Your income is not taxable') # If income is less that 20000 it ends
else: dep = int(input('Enter how many dependants you have: '))# Collects the number of dependants if needed
dedincome = income - stdded - (depded*dep) # Calculates the Income after deductions
incometax = dedincome * taxr # Calculates the amount of income tax taken out
gross = income - incometax # Calculates the gross and final income
print();#prints the tax information
print('Your income tax is $',incometax)
print('Your income after tax is $',gross)

The If is less than 20 should just print you dont get taxed but it is printing that then i get a error Traceback (most recent call last): File "filepath redacted/Lab6.py", line 7, in dedincome = income - stdded - (depded*dep) # Calculates the Income after deductions NameError: name 'dep' is not defined

I have tried indenting the lines, becasue its continueing to line 7 when it shouldnt, ive also looked for maybe a end command to put in place on line 5 but i cannot find anything.

  • 2
    `dep` is only defined when income >= 20000. – John Gordon Apr 06 '23 at 18:16
  • "but for some reason, the If else statement is triggering the calculations after the if when it should end when just printing" - I can't understand. **Why** do you think that the rest of the code should not run? "becasue its continueing to line 7 when it shouldnt" What is supposed to make it stop? – Karl Knechtel Apr 06 '23 at 19:25

1 Answers1

0

Python if-else blocks are indented. One liner if-else blocks assume the block consists of one single line. What you've written is essentially this.

taxr = 0.2#Sets the default tax rate
stdded = 10000 # sets the standard deduction
depded = 2000 # sets the dependent deduction
income = int(input("Enter your income: ")) # User input's their income

if income<20000: 
    print('Your income is not taxable') # If income is less that 20000 it ends
else: 
    dep = int(input('Enter how many dependants you have: '))# Collects the number of dependants if needed

dedincome = income - stdded - (depded*dep) # Calculates the Income after deductions
incometax = dedincome * taxr # Calculates the amount of income tax taken out
gross = income - incometax # Calculates the gross and final income
print();#prints the tax information
print('Your income tax is $',incometax)
print('Your income after tax is $',gross)

If you want the else block to extend to multiple lines, you must indent properly.

taxr = 0.2#Sets the default tax rate
stdded = 10000 # sets the standard deduction
depded = 2000 # sets the dependent deduction
income = int(input("Enter your income: ")) # User input's their income

if income<20000: 
    print('Your income is not taxable') # If income is less that 20000 it ends
else: 
    dep = int(input('Enter how many dependants you have: '))# Collects the number of dependants if needed
    dedincome = income - stdded - (depded*dep) # Calculates the Income after deductions
    incometax = dedincome * taxr # Calculates the amount of income tax taken out
    gross = income - incometax # Calculates the gross and final income
    print();#prints the tax information
    print('Your income tax is $',incometax)
    print('Your income after tax is $',gross)
SimonUnderwood
  • 469
  • 3
  • 12