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.
-
3Could 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).user1726343– user17263432013年11月22日 00:33:32 +00:00Commented Nov 22, 2013 at 0:33
-
1Nothing is wrong with your code. What's the problem?aIKid– aIKid2013年11月22日 00:39:12 +00:00Commented Nov 22, 2013 at 0:39
-
2I 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.Sean McSomething– Sean McSomething2013年11月22日 00:39:14 +00:00Commented 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 lotJerry Walker– Jerry Walker2013年11月22日 00:58:50 +00:00Commented Nov 22, 2013 at 0:58
2 Answers 2
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:
Don't use
listas a variable name; it's a built-in function, and you don't want to overwrite it.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.
camelCaseis generally denigrated;use_underscoresfor function names instead.
Comments
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.