Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么当我插入多个链接时,第一个链接总是打开,而其他链接不';T_Python_Python 3.x_If Statement_Url_Hyperlink - Fatal编程技术网

Python 为什么当我插入多个链接时,第一个链接总是打开,而其他链接不';T

Python 为什么当我插入多个链接时,第一个链接总是打开,而其他链接不';T,python,python-3.x,if-statement,url,hyperlink,Python,Python 3.x,If Statement,Url,Hyperlink,我正在编写一个程序,通过写入主题名称或教师名称,可以打开会议的链接,但当我写入教师名称时,程序中插入的第一个链接始终打开,而不是请求的链接。这就是代码: import webbrowser mess = input("Enter the teacher's name or subject:") if mess == "teacher1" or "physics": webbrowser.open("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”子句都是previous的精确否定,因此前两个条件块中的一个将始终执行,而其他块都无法到达。因此,请检查所有可接受的输入,只有当它们都与输入不匹配时,才打印“错误名称”。还要注意使用.lower()字符串方法使uer输入不区分大小写

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”或“physics”
的结果总是正确的

您可以尝试:

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")
或者,您可以简单地使用中的
操作符,将['teacher1','math']中的
mess=='teacher1'或mess=='math'
替换为
mess。这将为您提供以下信息:

   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")

“teacher1”或“physics”
这句话的计算结果总是正确的,所以这个if语句将被选中。非常感谢您,现在我试着,如果你想计算这个值是等于teacher1还是等于physics,那么你可以这样做:
如果mess=='teacher1'或mess=='physics':
那么剩下的部分你应该这样做,这能回答你的问题吗?