-2

I am trying to create a code library where I can store code snippets I find over time and retrieve them. Here is my code so far.

# Python Code Library User Interface

import sys

class CodeLib:
    codes = []
    option = ['help (-h)', 'list (-l)', 'import (-i)', 'quit (-q)']

#What does the user want to do?
    print "Welcome to the Code Library"
    print "What would you like to do? \n %s" % option
    choice = raw_input()
    print choice
    if choice == 'quit' or '-q':
        sys.exit()
    length = len(choice)
# Getting name of file to import if user chooses import before
# viewing the code list
    if choice[0] == 'i':
        _file_ = choice[6:length]
    elif choice[:2] == '-i':
        _file_ = choice[2:length]
# imports file if user chose import before viewing code list
    if choice[0:5] == 'import' or choice [0:2] == '-i':
        import _file_
# If user chose option other than import at beginning
# then perform option chosen then give ability to do something else
    while choice != 'quit' or '-q':
# provides basic help menu
        if choice == 'help' or '-h':
            print
            print '''(list or -l) brings up a list of code snippet names
(import or -i)/file_name brings up the code snippet you chose \n'''
# provides list of code files
        elif choice == 'list' or '-l':
            print codes
            print option[2], "file name"
            _file2_ = raw_input()
            import _file2_
# imports code file
        else:
            _file2_ = raw_input()
            import _file2_
# Next user option                  
        print "What would you like to do?"
        choice = raw_input

Now I know this is very incomplete so far. But the issue I am running into is that no matter what I enter as choice. The program does the first if statement, which exits the program. I have commented out the first if statement and tried again. But then no matter what I enter as choice. It performs this first if statement in the while loop and I just get stuck in a loop. So I'm not sure what is wrong but can someone help me out here?

RyPeck
  • 7,830
  • 3
  • 38
  • 58
Wickie Lee
  • 109
  • 1
  • 7
  • 1
    This gets asked so very many times. I wonder if it'd be asked less if "if" and "or" were easier to search for. – user2357112 Aug 13 '13 at 23:30
  • 1
    Also [if x or y or z == blah](http://stackoverflow.com/questions/15112125/if-x-or-y-or-z-blah) and about 100 other questions. – abarnert Aug 13 '13 at 23:30
  • Why do so many people coming from C++ or Java expect this to work? If you write `while (choice != "quit" || "-q")` in C++, it fails for exactly the same reason `while choice != "quit" or "-q"` does in Python… – abarnert Aug 13 '13 at 23:32
  • It doesn't exactly "fail". It just doesn't do what OP expects it to do. – Hyperboreus Aug 13 '13 at 23:34
  • @abarnert Because we say things like "if she's short or fat, I'm not interested." – 2rs2ts Aug 13 '13 at 23:37
  • Sorry for the dumb question. However I may not have been clear enough when. I am basically relearning to program altogether because it has been a long time since I have done anything in c or java – Wickie Lee Aug 13 '13 at 23:47
  • @2rs2ts That is the exact reason why I thought it should have worked the way I wrote it. But, as I said. I'm basically relearning everything. With just basic recollection of syntax and method names and a general idea of what they do – Wickie Lee Aug 14 '13 at 00:24
  • While everyone else has pointed out the way that the boolean operator works, I should also note that your last line should be `choice = raw_input()`. You have to call `raw_input` or else you won't actually prompt for input. The difference here is that you must use the parentheses (`()`) to call or invoke that function. You don't have them, so, without getting too technical, you're assigning the function itself to `choice`. If your code is working now, then you didn't paste it correctly and you do have those parentheses there. – 2rs2ts Aug 14 '13 at 01:03
  • Oh now i see what you are saying. My apologies on that. I must have left that out when pasting my code. the parentheses are there in my code. Sorry about the confusion and thanks for your input. – Wickie Lee Aug 14 '13 at 02:41

3 Answers3

7

The "or '-q'" part checks if the string '-q' is not false, which is true for any nonempty string. Change this line to:

if choice == 'quit' or choice == '-q':
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • 3
    OP should also check out the argparse module for this kind of thing, it eliminates a lot of needles if-thenning http://docs.python.org/2/library/argparse.html#module-argparse – theodox Aug 13 '13 at 23:31
  • I am going to look into that argparse module in a bit. Thank you forb the link! I realize all the if statments are a really...elementary way of doing things and I am trying to become more efficient – Wickie Lee Aug 14 '13 at 00:26
3

The condition choice == 'quit' or '-q' (and similar) always returns True, because '-q' evaulates to True and (True or Something) is always True.

You most probably wanted something like

choice == 'quit' or choice == '-q'

or

choice in ['quit', '-q']
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
0

First of all, you are creating an object in a way that I don't believe to be standard. Typically, you define methods for a class with def func(self,arg): blocks inside the class foo: block. The initializer method is indicated by the name __init__(self,args). Note that there are two '_' characters before and after init.

If you just want to run some code in a simple python script, you don't need it to be inside any block. You can just do something like:

print "Welcome to the Code Library"
print "What would you like to do? \n %s" % option
choice = raw_input()
print choice
if choice == 'quit' or '-q':
    sys.exit()
length = len(choice)
#More code goes here
cordoro
  • 97
  • 1
  • 7
  • 1
    This isn't really "non-standard". It is uncommon, and silly, and almost certainly not what the OP actually wants to do… but it work, and there are non-silly use cases for putting code (other than method definitions and class attribute assignments) directly in a class definition… – abarnert Aug 13 '13 at 23:49
  • 1
    Also, this isn't related to the OP's question or the problem he's trying to solve. – abarnert Aug 13 '13 at 23:49
  • Okay. thanks for the info on that. Ill get rid of the class for now until I understand more about methods and functions – Wickie Lee Aug 13 '13 at 23:50