0

I don't usually develop in Python but I have a few IDE's and Code Editors that I often use and switch between. To make it more convenient for myself, I thought I'd make a quick Python program that launches my IDE's/Editor's based on an input. The issue is, every time I run the program, the first if-statment always validates as true and runs that action.

Here is my code:

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

I'm not getting any errors but everytime I run this, the first if-statment always validates as true. So from this code, it keeps launching Notepad++. Can anyone tell what what I'm doing wrong? Thank you in advance!

tashad
  • 258
  • 2
  • 9

1 Answers1

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

or even better

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

Your original if statement will always evaluates to True because it is equivalent to:

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

and non-empty strings are casted to True in a logical operation.

See:

Benjamin Toueg
  • 10,511
  • 7
  • 48
  • 79
  • 3
    It would be great if you add an explanation why `launchCommand == "notepad" or launchCommand == "npp" or launchCommand == "n++" or launchCommand == "n"` always evaluates into `True` – Konstantin Apr 20 '15 at 13:15