If statement always runs when useing OR

Question:

If I run this using ‘or’ down there in the if statement it dose not matter what name I provide for the input, everything makes the function run. If I remove the or then the if statement runs and the elif and else are ignored.

   names = ['Luke', 'James', 'Bob', 'Rob', 'Vincent']
    emp_name = input("What is your name?")

    def employees():
        for name in names:
            if name == 'Luke':
                print(name, '- is the best')
                continue
            if name == 'Vincent':
                print(name, '- is insane')
                continue
            print(name, '- is okay')


    if emp_name.lower() == "luke e. hayes" or "luke":
        employees()
    elif emp_name.lower() != "luke e. hayes" or "luke":
        print("Get out")
    else:
        print("Error")
Asked By: Luke Hayes

||

Answers:

The issue is your if statement, you’re actually evaluating name==”luke e.hayes” or “luke” . “luke” will always evaluate to true since it has a value. Try

if emp_name.lower() == "luke e. hayes" or emp_name.lower() == "luke":

You will run into the same eact issue on the next line which should be converted to

 elif emp_name.lower() != "luke e. hayes" and emp_name.lower() !="luke":

The second one should print no matter what and you will want to use an and statment there. Let me know if you need more of an explanation

Answered By: Lucas Hendren
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.