我正在尝试编写一个函数,该函数接受用户输入的输入,并将输入返回为用逗号分隔的字符串,并在最后一个值之前加上“ and”。
theList = []
while True:
print('Enter your values' + '(Or enter nothing to stop)')
listItem = input()
if listItem == '':
break
theList = theList + [listItem]
def commaCode(x):
x.insert(-1, ('and' + x[-1]))
numbers = len(x)
spam = x[0]
for i in range(1, numbers):
finale = spam + ',' + x[i]
print(finale)
commaCode(theList)
“如果我输入:['jack','mack','lack',10,25]作为输入,我的输出应该是:” jack,mack,lack,10和25“
相反,我得到的只是输入的第一个值,例如“ jack”,然后输入的第二个值,然后是第一个值+'和'+最后一个值
Here is an example :
jack,mack
jack,lack
jack,10
jack,and25
jack,25