-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaste.py
More file actions
31 lines (28 loc) · 977 Bytes
/
Copy pathpaste.py
File metadata and controls
31 lines (28 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -*- coding: utf-8 -*-
'''
Created on Fri Jun 8 09:55:25 2018
@author: [email protected]
假定有下面这样的列表:
spam = ['apples', 'bananas', 'tofu', 'cats']
编写一个函数,它以一个列表值作为参数,返回一个字符串。
该字符串包含所有表项,表项之间以逗号和空格分隔,
并在最后一个表项之前插入 and。
例如,将前面的 spam 列表传递给函数,
将返回'apples, bananas, tofu, and cats'。
但你的函数应该能够处理传递给它的任何列表。
'''
def paste(List):
if type(List)==list:
length=len(List)
if length==0:
print('The list has none!')
elif length==1:
return str(List[0])
else:
s=''
for i in range(length-1):
s=s+str(List[i])+','
s=s+'and '+str(List[-1])+'.'
return s
else:
print('please input a list!')