Python Forum

Full Version: Calculation using output of if statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to calculate the amount of state tax withheld from a paycheck using a calculator coded with python below is the code i have wrote thus far:
if (Allowances == "0") :
StateLocalTax = 0.04206
elif (Allowances == "1") :
StateLocalTax = 0.00074
elif (Allowances == "2") :
StateLocalTax = 0.00079
elif (Allowances == "3") :
StateLocalTax = 0.00064
elif (Allowances == "4") :
StateLocal = 0.00056
elif (Allowances == "5") :
StateLocalTax = 0.00052
elif (Allowances == "6") :
StateLocalTax = 0.00048
elif (Allowances == "7") :
StateLocalTax = 0.00000
elif (Allowances == "8") :
StateLocalTax = 0.00000
elif (Allowances == "9") :
StateLocalTax = 0.00000
elif (Allowances == "10") :
StateLocalTax = 0.00000


StateLocal = float(GrossPay) * float(StateLocalTax)

Traceback (most recent call last):
File "C:/Users/19898/Desktop/Pay.py", line 35, in <module>
StateLocal = float(GrossPay) * float(StateLocalTax)
NameError: name 'StateLocalTax' is not defined
When I run the program it states that StateLocalTax is not defined. What am I missing?
Probably a variable scope problem. Can't tell without indentation or the full code to see how this is called. Could you post the full code, using the python tags?
HoursWorked = input('Hours:')
HourlyWage = input('Wage:$')
Allowances = input('Allowances:')

GrossPay = float(HoursWorked) * float(HourlyWage)

Medicare = float(GrossPay) * 0.0145

SocialSecurity = float(GrossPay) * 0.062

if (Allowances == "0") :
StateLocalTax = 0.04206
elif (Allowances == "1") :
StateLocalTax = 0.00074
elif (Allowances == "2") :
StateLocalTax = 0.00079
elif (Allowances == "3") :
StateLocalTax = 0.00064
elif (Allowances == "4") :
StateLocalTax = 0.00056
elif (Allowances == "5") :
StateLocalTax = 0.00052
elif (Allowances == "6") :
StateLocalTax = 0.00048
elif (Allowances == "7") :
StateLocalTax = 0.00000
elif (Allowances == "8") :
StateLocalTax = 0.00000
elif (Allowances == "9") :
StateLocalTax = 0.00000
elif (Allowances == "10") :
StateLocalTax = 0.00000


StateLocal = float(GrossPay) * float(StateLocalTax)

Federal = float(GrossPay) * 0.062

TotalTax = float(Medicare) + float(SocialSecurity) + float(StateLocal) + float(Federal)

NetPay = float(GrossPay) - float(TotalTax)

print (('SocialSecurity:$'), (SocialSecurity))
print (('Medicare:$'), (Medicare))
print (('Federal:$'), (Federal))
print (('StateLocal:$'), (StateLocal))
print (('Tax Deductions:$'), (TotalTax))
print (('Net Pay:$'), (NetPay))


There are 1 tab indentation starting with StateLocalTax in lines 12,14,16,18,20,22,24,26,28,30 and 32

HoursWorked = input('Hours:')
HourlyWage = input('Wage:$')
Allowances = input('Allowances:')

GrossPay = float(HoursWorked) * float(HourlyWage)

Medicare = float(GrossPay) * 0.0145

SocialSecurity = float(GrossPay) * 0.062

if (Allowances == "0") :
    StateLocalTax = 0.04206
elif (Allowances == "1") :
    StateLocalTax = 0.00074
elif (Allowances == "2") :
    StateLocalTax = 0.00079
elif (Allowances == "3") :
    StateLocalTax = 0.00064
elif (Allowances == "4") :
    StateLocalTax = 0.00056
elif (Allowances == "5") :
    StateLocalTax = 0.00052
elif (Allowances == "6") :
    StateLocalTax = 0.00048
elif (Allowances == "7") : 
    StateLocalTax = 0.00000
elif (Allowances == "8") :
    StateLocalTax = 0.00000
elif (Allowances == "9") :
    StateLocalTax = 0.00000
elif (Allowances == "10") :
    StateLocalTax = 0.00000
    

StateLocal = float(GrossPay) * float(StateLocalTax)

Federal = float(GrossPay) * 0.062

TotalTax = float(Medicare) + float(SocialSecurity) + float(StateLocal) + float(Federal)

NetPay = float(GrossPay) - float(TotalTax)

print (('SocialSecurity:$'), (SocialSecurity))
print (('Medicare:$'), (Medicare))
print (('Federal:$'), (Federal))
print (('StateLocal:$'), (StateLocal))
print (('Tax Deductions:$'), (TotalTax))
print (('Net Pay:$'), (NetPay))
I did not get an error:
Output:
Hours:4 Wage:$10.25 Allowances:2 SocialSecurity:$ 2.542 Medicare:$ 0.5945 Federal:$ 2.542 StateLocal:$ 0.03239 Tax Deductions:$ 5.710889999999999 Net Pay:$ 35.28911
Now, if you enter 11 as Allowances StateLocalTax will be undefined. You may be able to fix the problem by adding StateLocalTax=0 at line 10