반응형
# Make a function commafy that, given a list of three or more things,
# returns a list with commas.
#
# >>>> commafy(["trinket", "learning", "fun"])
# trinket, learning, and fun
# >>>> commafy(["lions", "tigers", "bears"])
# lions, tigers, and bears
def commafy(list):
# Add code here that returns the answer
objectCount = len(list)
tempList = ""
for i in range(objectCount - 1):
tempList += list[i] + ", "
tempList += "and " + list[i]
list = tempList
return list
# Add print statements here to test what your code does:
print (commafy(["trinket", "learning", "fun"]))
print (commafy(["lions", "tigers", "bears"]))
Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux trinket, learning, and learning lions, tigers, and tigers
Reference site : https://hourofpython.trinket.io/python-challenges#/string-challenges/oxford-comma-challenge
반응형
'<개인공부> - IT > [Python]' 카테고리의 다른 글
Python Dictionary 자료형 사용해보기 (0) | 2018.08.20 |
---|---|
Number of Things Challenge 함수 만들기 (0) | 2018.08.20 |
Gerund 판단 함수만들기 (0) | 2018.08.20 |
Reverse String 함수 만들기 (0) | 2018.08.20 |
isupper(), islower() 함수이용하여 문자열의 대소문자 변경하기 (0) | 2018.08.19 |