【问题标题】:python if + else if statementpython if + else if 语句
【发布时间】:2023-05-01 16:10:01
【问题描述】:

我是python新手,所以请帮忙,我不太了解。

我正在做一个请求命令的项目,如果命令是 = “帮助”,那么它会说明如何使用该程序。我似乎无法做到这一点,每次我尝试使用 if 语句时,它仍然会打印帮助部分,无论该命令是否存在。

示例:有人输入了脚本中不存在的命令,它仍会打印帮助部分。

print("welcome, to use this, please input the options below")
print ("help  |  exit")


option = input("what option would you like to use? ")

if help:
    print("this is a test, there will be an actual help section soon.")
else:
    print("no such command")
    

【问题讨论】:

标签: python if-statement command


【解决方案1】:

使用==相等运算符检查

print("welcome, to use this, please input the options below")
print("help  |  exit")


option = input("what option would you like to use? ")

if option == "help":
    print("this is a test, there will be an actual help section soon.")
else:
    print("no such command")

【讨论】:

    【解决方案2】:

    您需要在if 语句中引用您的变量option 才能使其工作。

    print("welcome, to use this, please input the options below")
    print ("help  |  exit")
    
    
    option = input("what option would you like to use? ")
    
    if option == "help":
        print("this is a test, there will be an actual help section soon.")
    else:
        print("no such command")
    

    我也是刚开始接触 Python,希望我们都能从这里学到很多东西。

    【讨论】:

      【解决方案3】:

      试试这个。

       `print("welcome, to use this, please input the options below")
      print ("help  |  exit")
      
      
      option = input("what option would you like to use? ")
      
      if option == "help":
          print("this is a test, there will be an actual help section soon.")
      else:
          print("no such command")`
      

      【讨论】: