我正在做一个简单的程序,创建一个购物清单。现在,我无法将空白输入添加到我的列表中:当我用空格键或空格键输入时,它会将空白输入添加为项目。有没有简单的方法来防止这种情况?制作一个简单的程序,如何让它如此空白/空格输入不计数/添加到它
例如像这样的容错性:
#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()
我不熟悉python,但是当我尝试使用python 2.7.10运行它时,它给出了一个语法错误 - 是否允许声明一个名为'print'的函数,这是一个内置函数?或者是OP的错误? –