I am making a simple program that creates a grocery list. Right now, I am having trouble with blank input being added to my list: when I hit enter with or without spaces, it adds the blank input as an item. Is there a simple way to prevent this?
e.g. something like this as a fault tolerance:
#Enter your item or command:
#Shopping items cannot be blank.
#Enter your item or command:
#Shopping list items cannot be blank.
Current code:
List = []
def Menu():
print('Here is a list of options:', '\n P : Print the List',
'\n C : Empty the List', '\n E : Exit',
'\n R : Print this command list')
def add(item):
List.append(item)
print("{0} has been added to the list".format(item))
# Having trouble here: I need to make it check against empty spaces and
# not add to the list
def listInput():
item = input('Enter an item or command: ')
print('You have {0} items on your list.'.format(len(List)))
return item
def print():
print('Your shopping list:')
for i in List:
print(" * {0}".format(i))
def clear():
del List[:]
print('All items removed from list.')
print('You have 0 items on your list.')
def start():
print('Welcome to the your Shopping List Program')
def end():
print('Thank you for using your Shopping List Program.')
def main():
start()
Menu()
item = listInput()
while item != 'E':
if item == 'P':
Print()
elif item == 'R':
Menu()
elif item == 'C':
clear()
else:
add(item)
item = listInput()
end()
main()