Python program decision control issue

Question:

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?

Asked By: Wickie Lee

||

Answers:

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':
Answered By: BartoszKP

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']
Answered By: Hyperboreus

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
Answered By: cordoro
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.