2017-05-26 36 views
0

我正在做一个简单的程序,创建一个购物清单。现在,我无法将空白输入添加到我的列表中:当我用空格键或空格键输入时,它会将空白输入添加为项目。有没有简单的方法来防止这种情况?制作一个简单的程序,如何让它如此空白/空格输入不计数/添加到它

例如像这样的容错性:

#Enter your item or command: 
#Shopping items cannot be blank. 
#Enter your item or command: 
#Shopping list items cannot be blank. 

当前代码:

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

我不熟悉python,但是当我尝试使用python 2.7.10运行它时,它给出了一个语法错误 - 是否允许声明一个名为'print'的函数,这是一个内置函数?或者是OP的错误? –

回答

0

只有添加的项目,如果它不是一个空字符串(删除空格),要做到这一点,您需要除去else:在等待非空输入保护

elif item.strip() != '': 
    add(item) 
1

看跌。下面是一个简单的版本:

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 
+0

“.strip()”的用途究竟是什么,如果没有它,它会起作用吗? – TheGirrafish

+0

'while item.strip()'更好 – C8H10N4O2

+0

@ TheGirrafish:strip允许线上有空白。 – Prune

0

使用下面的代码来修剪你的字符串,

myString.strip() 
0

这里有一个选项:

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 
+0

'len()'中的'len而不是len(item)'是不必要的,因为空字符串被认为是false。为了稍微更健壮,我建议这样做:'while not item.strip()',因为这样可以防止添加空格。但是,如果你想删除多余的空格,这可能会更好:'item = input('输入一个项目或命令:')''而不是项目:'' –

1

这里其他的答案做一个好工作更直接回答你的问题,但我建议稍微改写一下,而不仅仅是眼前的问题。

这里是您当前main()定义:

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

这里是我建议你把它改写:

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.') 

它应该是自我解释为它做什么(添加评论如果您需要澄清!),但重点是在阅读代码时更容易了解发生的情况,并且还可以消除多余的行,如两次使用item = listInput()

当然,这将需要listInput()轻微改写为好,但它也让我们解决你的问题的一个稍微更优雅的方式:

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

再次,请让我知道如果您有任何疑问,因为我认为代码本身就说明了,而且不言自明!

相关问题