0

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()
Prune
  • 76,765
  • 14
  • 60
  • 81
Tye Chamberlain
  • 79
  • 1
  • 1
  • 2
  • I'm not familiar with python, but when I try to run this with python 2.7.10 it gives a syntax error- are you allowed to declare a function called `print`, which is a builtin? Or is that an error by OP? – Karl Reid May 26 '17 at 18:04

4 Answers4

1

Put in a guard that waits for non-empty input. Here is a simple version:

def listInput():
    item = ""
    while item.strip() == "":
        item = input('Enter an item or command: ')
    print('You have {0} items on your list.'.format(len(List)))
    return item
Prune
  • 76,765
  • 14
  • 60
  • 81
1

The other answers here do a good job of more directly answering your question, but I'd recommend a slight rewrite of a little more than just the immediate problem.

Here is your current main() definition:

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()

Here is how I would recommend you rewrite it:

def main():
    start()
    Menu()

    item = None
    while item != 'E':

        print('You have {0} items on your list.'.format(len(List)))
        item = listInput()

        if item == 'P':
            Print()

        elif item == 'R':
            Menu()

        elif item == 'C':
            clear()

        elif item == 'E':
            end()

        elif item is not None:
            add(item)

        else:  # item is None  -- this last else and print are optional
            print('Shopping items cannot be blank.')

It should be pretty self explanatory as to what it does (add a comment if you need clarification!), but the point is that it's easier to follow what's happening when you read the code, and you also eliminate redundant lines like having item = listInput() twice.

This will of course require a slight rewrite of listInput() as well, but it also allows us a slightly more elegant way of solving your problem:

def listInput():
    item = input('Enter an item or command: ').strip()
    if not item:
        item = None
    return item

Again, please let me know if you have questions, as I think the code speaks for itself and is fairly self-explanatory!

B. Eckles
  • 1,626
  • 2
  • 15
  • 27
0

Only add the item if it is not an empty string (with spaces removed), to do this you'll need to remove the else:

elif item.strip() != '':
    add(item)
Nick is tired
  • 6,860
  • 20
  • 39
  • 51
0

Here is one option:

def listInput():
    item = input('Enter an item or command: ')
    while not len(item):
        item = input('Shopping items cannot be blank. Enter your item or command: ')
    print('You have {0} items on your list.'.format(len(List)))
    return item
  • `len()` in `while not len(item)` is unnecessary, as the empty string is considered false. For slightly more robustness, I'd recommend this: `while not item.strip()`, as this will prevent whitespace from being added too. In case you want to remove extra whitespace anyway, however, this might be even better: `item = input('Enter an item or command: ')` `while not item:` – B. Eckles May 26 '17 at 18:53