본문 바로가기
<개인공부> - IT/[Python]

Commafy 함수 만들기

by Aggies '19 2018. 8. 20.
반응형
# 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

반응형