반응형
# Make a function how_many that, given a list of a number
# and a thing name, returns a grmmatically correct sentence
# describing the number of things.
#
# >>>> how_many([5, "trinket"])
# There are 5 trinkets.
# >>>> how_many([1, "king"])
# There is 1 king.
def how_many(the_list):
# Add code here that returns the answer
sentence = ""
for i in range(len(the_list)):
if(i == 0):
sentence += str(the_list[i])
else:
sentence += " " + the_list[i]
if(the_list[0] == 1):
sentence = "There is " + sentence
else:
sentence = "There are " + sentence
the_list = sentence
return the_list
# Add print statements here to test what your code does:
print (how_many([5, "trinket"]))
print (how_many([1, "king"]))
Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux There are 5 trinket There is 1 king
Reference site : https://hourofpython.trinket.io/python-challenges#/string-challenges/number-of-things-challenge
반응형
'<개인공부> - IT > [Python]' 카테고리의 다른 글
List 자료형의 Join함수 사용하기 (0) | 2018.08.21 |
---|---|
Python Dictionary 자료형 사용해보기 (0) | 2018.08.20 |
Commafy 함수 만들기 (0) | 2018.08.20 |
Gerund 판단 함수만들기 (0) | 2018.08.20 |
Reverse String 함수 만들기 (0) | 2018.08.20 |