Python Forum
Calculation using output of if statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculation using output of if statement
#1
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?
Reply
#2
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?
Reply
#3
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))
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Common understanding of output processing with conditional statement neail 6 875 Sep-17-2023, 03:58 PM
Last Post: neail
  Help with output from if statement fgaascht 6 1,948 Jan-17-2022, 02:53 PM
Last Post: perfringo
  Passing print output into another print statement Pleiades 6 3,148 Sep-08-2019, 02:37 PM
Last Post: Pleiades
  Unexpected output: if statement CabbageMan 1 1,760 Sep-04-2019, 04:12 PM
Last Post: ThomasL
  Trying to code backwords in if statement for different output in some scenarios skrivver99 1 2,432 Dec-03-2018, 01:32 AM
Last Post: Windspar

Forum Jump:

User Panel Messages

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