python if + else if 语句

python if + else if statement

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

使用 == 相等运算符进行检查

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

您需要在 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,我希望我们都能从这里学到很多东西。

试试这个。

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