0

hey im making a simple little grocery list on Python. I know it's not the most eloquent... but I am just learning the syntax right now. I want to get into learning Django.

list = []

def makeList():
    listing = True
    while listing:
        addTo = raw_input("Add to list: ")

        if addTo == 'q':
            listing = False
        else:
            list.append(addTo)

def checkList():
    if check in list:
        print "Yay there is " + check + " here"
    else:
        print "No you have not added that..."
        addAnother = raw_input("Would you like to add it? ")
        if str.lower(addAnother) == "yes":
            list.append(check)
        elif str.lower(addAnother) == "no":
            print "Okay then here is your list."
            print list
        else:
            print check

makeList()

check = raw_input("What item: ")

checkList()

I know its pretty complex and hard to understand O_o... but you can see that the nested if statement is not registering when you run it.

What is making it do this? I think that's the best way to ask this.

4
  • 3
    Could you please add your output? It is difficult to actually tell what the problem is until we know what the code outputs (and what it should output). Commented Nov 22, 2013 at 0:33
  • 1
    Nothing is wrong with your code. What's the problem? Commented Nov 22, 2013 at 0:39
  • 2
    I just ran it & it works fine. You might want to explain what behavior you're seeing & what you're expecting to help people figure out what's wrong. Maybe you're expecting it to act like there's a loop when one doesn't exist. Maybe you've mixed up tabs and spaces and your indentation is broken but there's nothing obviously wrong with your code from here. Commented Nov 22, 2013 at 0:39
  • I got it. Sorry you guys, that was a waste of time. I forgot to add a print statement under the would you like to add part... thanks a lot Commented Nov 22, 2013 at 0:58

2 Answers 2

1

I've rewritten it a bit to make it cleaner and more Pythonic;

def get_list(prompt, halt):
    lst = []
    while True:
        item = raw_input(prompt)
        if item == halt:
            return lst
        else:
            lst.append(item)

def check_list(lst, item):
    if item in lst:
        print('Yay there is {} here'.format(item))
        return True
    else:
        print('No you have not added {}'.format(item))
        return False

def get_yesno(prompt):
    while True:
        yesno = raw_input(prompt).lower()
        if yesno in {'y', 'yes'}:
            return True
        elif yesno in {'n', 'no'}:
            return False

def main():
    mylist = get_list('Add to list:', 'q')

    check = raw_input('Look for item:')
    if not check_list(mylist, check):
        if get_yesno('Would you like to add it?'):
            mylist.append(check)

    print(mylist)

if __name__=="__main__":
    main()

Some style tips:

  1. Don't use list as a variable name; it's a built-in function, and you don't want to overwrite it.

  2. Global variables are almost always a bad idea; passing data around explicitly makes it much easier to figure out where bad data is coming from, and makes functions more reusable.

  3. camelCase is generally denigrated; use_underscores for function names instead.

Sign up to request clarification or add additional context in comments.

Comments

0

You probably intended to keep going rather than break when you append the new item (or at least print something to indicate success), but the nested if statement works just fine, appends the thing to the list as specified and then the function and program terminate.

2 Comments

Not much of an answer? And where did you get that conclusion?
I inferred what the most likely problem is from what the code snippet might be intended to do. If that assumption is wrong, fine, but I see no useful didactic purpose to say "your code works fine" and ignore probable discrepancies between code and intention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.