-1

I am compiling a program that, by writing the name of the subject or the name of the teacher, opens the link for the meeting, but when I write the name of a teacher, the first link inserted in the program always opens and not the one requested. That's the code:

import webbrowser
    
mess = input("Enter the teacher's name or subject:")

if mess == "teacher1" or "physics":
    webbrowser.open("link of teacher1")
elif mess != "teacher1" or "physics":
    print("Invalid name")
    input("Press ENTER to exit")

elif mess == "teacher2" or "chemistry":
    webbrowser.open("link of teacher2")
elif mess != "teacher2" or "chemistry":
    print("Invalid name")
    input("Press ENTER to exit")

elif mess == "teacher3" or "Math":
    webbrowser.open("link of teacher3")
elif mess != "tacher3" or "Math":
    print("Invalid name")
    input("Press ENTER to exit")
heerkole
  • 3
  • 3
  • 1
    `"teacher1" or "physics"` this always evaluates to True so this if statement will be chosen always – Matiiss Apr 14 '21 at 17:04
  • Thank you so much, now I try – heerkole Apr 14 '21 at 17:06
  • if You want to evaluate if the value equals teacher1 or the value equals physics do this: `if mess == 'teacher1' or mess == 'physics':` and so You should do for the rest too – Matiiss Apr 14 '21 at 17:08
  • 1
    Does this answer your question? [How to test multiple variables against a single value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-single-value) – Maurice Meyer Apr 14 '21 at 17:27

2 Answers2

0

First of all, your or expression always evaluates to True, as strings are interpreted as True if they have length greater than 0. Also note that every your 'elif' clause is exact negation of previous, so one of first two conditional blocks will be always executed and none of other blocks can be reached. So check for all your acceptable input and print 'Wrong name' only if none of them matched the input. Also note the use of .lower() string method to make uer input case insensitive.

import webbrowser

mess = input("Enter the teacher's name or subject: ").lower()

if mess in {"teacher1", "physics"}:
    webbrowser.open("link of teacher1")
elif mess in {"teacher2", "chemistry"}:
    webbrowser.open("link of teacher2")
elif mess in {"teacher3", "math"}:
    webbrowser.open("link of teacher3")
else:
    print("Invalid name")
    input("Press ENTER to exit")
STerliakov
  • 4,983
  • 3
  • 15
  • 37
0

There are two ways you can handle this. As @heerkole pointed out, your statement 'teacher1' or 'physics' always evaluates to True.

You can try:

import webbrowser
    
mess = input("Enter the teacher's name or subject:")

if mess == "teacher1" or mess == "physics":
    webbrowser.open("link of teacher1")
elif mess == "teacher2" or mess == "chemistry":
    webbrowser.open("link of teacher2")
elif mess == "teacher3" or mess == "Math":
    webbrowser.open("link of teacher3")
else:
    print("Invalid name")
    input("Press ENTER to exit")

Or you can simply use the in operator and replace mess == 'teacher1' or mess == 'math' with mess in ['teacher1', 'math']. This gives you the following:

   import webbrowser
        
    mess = input("Enter the teacher's name or subject:")
    
    if mess in ["teacher1", "physics"]:
        webbrowser.open("link of teacher1")
    elif mess in ["teacher2", "chemistry"]:
        webbrowser.open("link of teacher2")
    elif mess in ["teacher", "Math"]:
        webbrowser.open("link of teacher3")
    else:
        print("Invalid name")
        input("Press ENTER to exit")
Shahan M
  • 523
  • 1
  • 7
  • 18