【发布时间】:2015-10-19 14:35:29
【问题描述】:
我正在尝试生成一个列表 python。在这个程序中,我正在尝试编写一个函数,该函数从键盘获取列表值并返回一个字符串,其中所有项目由逗号和空格分隔,并在最后一项之前插入 "and"。 例如,将前一个垃圾邮件列表传递给函数将返回 '苹果、香蕉、豆腐和猫'。她是我的密码
spam=[]
def rock(val):
while True:
print("Enter the text: ")
val=input()
spam.append(val)
return spam
print("Enter the number: ")
n=int(input())
for i in range(0,n):
s=', '.join(rock(''))
print(s)
Output:
Enter the number:
3
Enter the text:
rock
rock
Enter the text:
dog
rock, dog
Enter the text:
cad
rock, dog, cad
现在,在上面提到的程序中,我已经成功生成了一个用逗号分隔的列表。但我不知道如何将 "and" 放在最后一个值之前。像这样,'苹果、香蕉、豆腐和猫'
【问题讨论】:
标签: python python-2.7 python-3.x