Making a simple program, how to I make it so blank/spaces input doesn't count/add to it
本问题已经有最佳答案,请猛点这里访问。
我正在制作一个简单的程序,创建一个杂货清单。现在,我把空白输入添加到列表中遇到了麻烦:当我输入有或没有空格时,它将空白输入添加为项。有没有一个简单的方法来防止这种情况?
例如,像这样的容错:
1 2 3 4 | #Enter your item or command: #Shopping items cannot be blank. #Enter your item or command: #Shopping list items cannot be blank. |
当前代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | List = [] def Menu(): print('Here is a list of options:', ' P : Print the List', ' C : Empty the List', ' E : Exit', ' 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() |
这里的其他答案可以更直接地回答您的问题,但是我建议您稍微重写一下,而不仅仅是直接的问题。
这是您当前的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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() |
以下是我建议您重写的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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.') |
对于它的作用应该是非常不言自明的(如果需要澄清的话,请添加一条评论!)但重点是,当您读取代码时,更容易跟踪正在发生的事情,而且您还消除了多余的行,如使用
当然,这也需要稍微重写
1 2 3 4 5 | def listInput(): item = input('Enter an item or command: ').strip() if not item: item = None return item |
再次,请让我知道如果你有问题,因为我认为代码本身就说明了,而且是相当不言自明的!
放入一个等待非空输入的保护。下面是一个简单的版本:
1 2 3 4 5 6 | 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 |
这里有一个选项:
1 2 3 4 5 6 | 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 |
仅当项目不是空字符串时才添加(删除空格),为此,需要删除
1 2 | elif item.strip() != '': add(item) |