-2

I've been trying to solve this problem in my class but I cant seem to get the math right.

Using For.. loop, Write a program to fetch the name, salary and the state of 5 employees. Calculate the federal tax, state tax and the net salary for each employee.

statetax = 0
fedtax = 0
employsalary = 0
netsalary = 0

for people in range(5):
    employname = input("Please enter employee name: ")
    employsalary = int(input("Please enter employee salary: "))
    employstate = input("Please enter employee state: ")
    if employsalary >= 100000:
        fedtax = (employsalary * 20) / 100
    else: # employsalary < 100000            
        fedtax = (employsalary * 15) / 100
    if employstate == 'California' or 'Neveda' or 'Arizona' or 'Washington':
        statetax = (employsalary * 10) / 100
    elif employstate == 'Texas' or 'NewMexico' or 'Alabama':
        statetax = (employsalary * 9) / 100
    elif employstate == 'NewYork' or 'Illinois' or 'Wisconsin' or 'Delaware':
        statetax = (employsalary * 8) / 100
    else:
        statetax = (employsalary * 12) / 100
    netsalary = (employsalary - fedtax - statetax)

    print(employname + ' federal tax is: ' +str(fedtax))
    print(employname + ' state tax is: ' +str(statetax))
    print(employname + ' net salary is: ' +str(netsalary))

for some reason the calculations are not correct when the value of employsalary is multiplied by both 8 and 9 in the two different statements and I cant figure out why. I hope I explained this right. x_x

Gilseung Ahn
  • 2,598
  • 1
  • 4
  • 11
  • Hint: That is not how an `or` works. – Saelyth Apr 08 '20 at 04:15
  • Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value). See also [AND/OR in python](https://stackoverflow.com/questions/10149747/and-or-in-python), and [Or conditional in python troubles](https://stackoverflow.com/questions/17375793/or-conditional-in-python-troubles) – dspencer Apr 08 '20 at 04:38

2 Answers2

1

Every if state should be changed the following manner.

Change if employstate == 'California' or 'Neveda' or 'Arizona' or 'Washington': to

if employstate == 'California' or employstate == 'Neveda' or employstate == 'Arizona' or employstate == 'Washington':

Or, it is another option.

if employstate in ['California', 'Neveda', 'Arizona', 'Washington']:

String is always true when it is not null, and thus if employstate == 'California' or 'Neveda' equals to if employstate == 'California'.

Gilseung Ahn
  • 2,598
  • 1
  • 4
  • 11
0

Refer this

statetax = 0
fedtax = 0
employsalary = 0
netsalary = 0

for people in range(5):
    employname = input("Please enter employee name: ")
    employsalary = int(input("Please enter employee salary: "))
    employstate = input("Please enter employee state: ")
    if employsalary >= 100000:
        fedtax = (employsalary * 20) / 100
    else:
        employsalary < 100000
        fedtax = (employsalary * 15) / 100
    if (employstate == 'California') or (employstate == 'Neveda') or (employstate == 'Arizona') or (employstate == 'Washington'):
        statetax = (employsalary * 10) / 100
    elif (employstate == 'Texas') or (employstate == 'NewMexico') or (employstate == 'Alabama'):
        statetax = (employsalary * 9) / 100
    elif (employstate == 'NewYork') or (employstate == 'Illinois') or (employstate == 'Wisconsin') or (employstate == 'Delaware'):
        statetax = (employsalary * 8) / 100
    else:
        statetax = (employsalary * 12) / 100
    netsalary = (employsalary - fedtax - statetax)

    print(employname + ' federal tax is: ' +str(fedtax))
    print(employname + ' state tax is: ' +str(statetax))
    print(employname + ' net salary is: ' +str(netsalary))

A == B or C or D is not a valid syntax.
A == B or A == C or A == D is valid in your case.