Search code examples
pythonstringif-statementcomparison-operators

Last else if statement does not print


I am trying to get the output to display "Students do not get keys" if the role is "student", "Administrators and teachers get keys" if the role is either "administrator" or "teacher", and "You can only be administrator, teacher, or student" if the role is none of those options. With the code below, the first if-statement and elif-statement correctly display that "Students do not get keys" if the user types "student", and that "Administrators and teachers get keys" if the user types "administrator" or "teacher". But if the user were to write "janitor", the output would still display "Administrators and teachers get keys". I have already tried adding a second elif-statement at the end instead of and else: and then if...: below it, but the results were the same. I also tried mixing the order of the if statements around, but the output would never print "You can only be an administrator, teacher, or student". What can I do? Thank you.

role = input("Your role: ")

if role == "student":
    print("Students do not get keys!")
elif role == "administrator" or "teacher":
    print("Administrators and teachers get keys!")
else:
    if role != "administrator" or "teacher" or "student":
        print("You can only be an administrator, teacher, or student!")

Solution

  • Try this:

    role = input("Your role: ")
    print(role)
    if role == "student":
        print("Students do not get keys!")
    elif role == "administrator" or role =="teacher":
        print("Administrators and teachers get keys!")
    elif role != "administrator" or role!= "teacher" or role!="student":
            print("You can only be an administrator")
    

    This works a little better putting all the values in a list

    role = input("Your role: ")
    print(role)
    roles = ["student","teacher","administrator"]
    if role == "student":
        print("Students do not get keys!")
    elif role == "administrator" or role =="teacher":
        print("Administrators and teachers get keys!")
    elif role not in roles:
            print("You can only be an administrator")