为什么当我插入多个 link 时,第一个总是打开而其他的不打开?
Why when I insert more than one link, the first one always opens and the others don't?
我正在编写一个程序,通过写入主题名称或教师姓名,打开会议的 link,但是当我写入教师姓名时,第一个 link 插入程序总是打开而不是请求的那个。那是代码:
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")
首先,您的 or 表达式的计算结果始终为 True,因为如果字符串的长度大于 0,则字符串将被解释为 True。另请注意,您的每个 'elif' 子句都是前一个子句的精确否定,因此一个将始终执行前两个条件块的一部分,并且可以到达其他块的 none 个。因此,检查所有可接受的输入并仅当 none 与输入匹配时才打印 'Wrong name'。另请注意使用 .lower() 字符串方法使用户输入不区分大小写。
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")
有两种方法可以解决这个问题。正如@heerkole 指出的那样,您的陈述 'teacher1' or 'physics'
始终评估为 True。
你可以试试:
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")
或者您可以简单地使用 in
运算符并将 mess == 'teacher1' or mess == 'math'
替换为 mess in ['teacher1', 'math']
。这为您提供了以下内容:
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")
我正在编写一个程序,通过写入主题名称或教师姓名,打开会议的 link,但是当我写入教师姓名时,第一个 link 插入程序总是打开而不是请求的那个。那是代码:
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")
首先,您的 or 表达式的计算结果始终为 True,因为如果字符串的长度大于 0,则字符串将被解释为 True。另请注意,您的每个 'elif' 子句都是前一个子句的精确否定,因此一个将始终执行前两个条件块的一部分,并且可以到达其他块的 none 个。因此,检查所有可接受的输入并仅当 none 与输入匹配时才打印 'Wrong name'。另请注意使用 .lower() 字符串方法使用户输入不区分大小写。
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")
有两种方法可以解决这个问题。正如@heerkole 指出的那样,您的陈述 'teacher1' or 'physics'
始终评估为 True。
你可以试试:
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")
或者您可以简单地使用 in
运算符并将 mess == 'teacher1' or mess == 'math'
替换为 mess in ['teacher1', 'math']
。这为您提供了以下内容:
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")