多个Python ifstaments?

2024-10-01 22:44:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我通常不使用Python开发,但我有一些IDE和代码编辑器,我经常使用它们并在它们之间切换。为了方便自己,我想我应该制作一个快速的Python程序,根据输入启动IDE/Editor。问题是,每次运行程序时,第一个if语句总是验证为true并运行该操作。在

这是我的代码:

import os

#NOTE: I have trimmed the root directories here to save space. Just removed the subfolder names, but the programs are the same. 

notepadPlusPlusLaunch = "C:\\Notepad++\\notepad++.exe"
bracketsLaunch = "C:Brackets\\Brackets.exe"
aptanaLaunch = "C:Aptana Studio\\AptanaStudio3.exe"
devCppLaunch = "C:Dev-Cpp\\devcpp.exe"
githubLaunch = "C:GitHub,  Inc\\GitHub.appref-ms"
androidLaunch = "C:android-studio\\bin\\studio64.exe"
ijLaunch = "C:bin\\idea.exe"
pycharmLaunch = "C:JetBrains\\PyCharm  4.0.5\\bin\\pycharm.exe"
sublimeLaunch = "C:Sublime Text 3\\sublime_text.exe"

def launcherFunction(command):
    os.startfile(command)

launchCommand = input("")

if launchCommand == "notepad" or "npp" or "n++" or "n":
    launcherFunction(notepadPlusPlusLaunch)

elif launchCommand == "brackets" or "b":
    launcherFunction(bracketsLaunch)

elif launchCommand == "aptana" or "as" or "webide":
    launcherFunction(aptanaLaunch)

elif launchCommand == "dcpp"or "c++":
    launcherFunction(devCppLaunch)

elif launchCommand == "gh" or "github" or "git" or "g":
    launcherFunction(githubLaunch)

elif launchCommand == "android" or "a" or "as":
    launcherFunction(androidLaunch)

elif launchCommand == "java" or "ij" or "idea" or "j":
    launcherFunction(ijLaunch)

elif launchCommand == "python" or "pc" or "p":
    launcherFunction(pycharmLaunch)

elif launchCommand == "code" or "sublime" or "html" or " " or "s" or "php" or "css" or "js" or "jquery":
    launcherFunction(sublimeLaunch)

elif launchCommand == "help":
        print(notepadPlusPlusLaunch, "\n", bracketsLaunch, "\n", aptanaLaunch, "\n", devCppLaunch, "\n",githubLaunch, "\n", androidLaunch, "\n", ijLaunch, "\n",     pycharmLaunch, "\n", sublimeLaunch, "\n", musicLaunch,"\n")

else: 
    print("Invalid Entry")

我没有得到任何错误,但每次我运行这个,第一个if语句总是验证为真。所以从这段代码中,它继续启动Notepad++。谁知道我做错了什么吗?提前谢谢你!在


Tags: orthe代码ifbinexeeliflaunchcommand
1条回答
网友
1楼 · 发布于 2024-10-01 22:44:17
if launchCommand == "notepad" or launchCommand == "npp" or launchCommand == "n++" or launchCommand == "n":

甚至更好

^{pr2}$

原始if语句的计算结果始终为True,因为它等效于:

if (launchCommand == "notepad") or ("npp") or ("n++") or ("n"):

在逻辑操作中,非空字符串被强制转换为True。在

参见:

相关问题 更多 >

    热门问题