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

Number of Things Challenge 함수 만들기

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

반응형