【发布时间】:2017-06-15 18:28:31
【问题描述】:
我正在处理“Automate the Boring Stuff with Python”。我不知道如何从下面的程序中删除最终输出逗号。目标是不断提示用户输入值,然后将其打印在列表中,并在末尾插入“and”。输出应如下所示:
apples, bananas, tofu, and cats
我的看起来像这样:
apples, bananas, tofu, and cats,
最后一个逗号把我逼疯了。
def lister():
listed = []
while True:
print('type what you want to be listed or type nothing to exit')
inputted = input()
if inputted == '':
break
else:
listed.append(inputted+',')
listed.insert(-1, 'and')
for i in listed:
print(i, end=' ')
lister()
【问题讨论】: