2015-04-20 38 views
0

我通常不用Python進行開發,但是我經常使用和切換一些IDE和代碼編輯器。爲了讓自己更方便,我想我會製作一個快速的Python程序,根據輸入啓動我的IDE /編輯器。問題是,每次運行程序時,第一個if-statment總是被驗證爲true並運行該操作。多個Python if-statments?

這裏是我的代碼:

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-statment始終驗證爲真。所以從這段代碼中,它一直在啓動Notepad ++。任何人都可以告訴我做錯了什麼嗎?先謝謝你!

+1

'如果launchCommand == 「記事本」 或 「NPP」 或 「n ++」 或 「N」'是錯誤 –

+0

可能會有所幫助: [Python新手:「如果X == Y和Z」語法](http://stackoverflow.com/questions/3629586/python-newbie-if-xy-and-z-syntax) – soon

+0

這可能會幫助你理解,類型這到你的解釋器:'如果'你好':打印('是')' –

回答

7
if launchCommand == "notepad" or launchCommand == "npp" or launchCommand == "n++" or launchCommand == "n": 

甚至更​​好

if launchCommand in ("notepad", "npp", "n++", "n"): 

你原來的if語句總是會評估爲True因爲它相當於:

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

和非空字符串被鑄造到True在邏輯運算。

參見:

+3

如果你添加一個解釋,爲什麼'launchCommand ==「notepad」或launchCommand ==「npp」或launchCommand ==「n ++」或launchCommand ==「n」'總是評估爲「True」 – Alik