【问题标题】:Why when I insert more than one link, the first one always opens and the others don't?为什么当我插入多个链接时,第一个总是打开而其他的却没有?
【发布时间】:2021-07-09 18:11:28
【问题描述】:

我正在编译一个程序,通过编写主题名称或教师姓名,打开会议的链接,但是当我编写教师姓名时,程序中插入的第一个链接总是打开而不是请求的那个。代码如下:

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

【问题讨论】:

  • "teacher1" or "physics" this 总是计算为 True 所以这个 if 语句将始终被选中
  • 非常感谢,现在我试试
  • 如果您想评估该值是否等于teacher1 或该值是否等于物理,请执行以下操作:if mess == 'teacher1' or mess == 'physics':,其余的您也应该这样做

标签: python python-3.x if-statement url hyperlink


【解决方案1】:

首先,您的 or 表达式始终计算为 True,因为如果字符串的长度大于 0,则将其解释为 True。还要注意,您的每个 'elif' 子句都是前一个的完全否定,所以前两个条件之一块将始终被执行,并且无法到达任何其他块。因此,请检查所有可接受的输入并仅在它们都不匹配输入时打印“错误名称”。还要注意使用 .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")

【讨论】:

    【解决方案2】:

    有两种方法可以处理这个问题。正如@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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多