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

Gerund 판단 함수만들기

by Aggies '19 2018. 8. 20.
반응형
# Make a function gerund_infinitive that, given a string ending in "ing",
# returns the rest of the string prefixed with "to ". If the string
# doesn't end in "ing", return "That's not a gerund!"
#
# >>>> gerund_infinitive("building")
# to build
# >>>> gerund_infinitive("build")
# That's not a gerund!

def gerund_infinitive(string):
# Add code here that returns the answer
stringLength = len(string)
isGerund = ""
for i in range(stringLength - 3, stringLength):
isGerund += string[i]
if(isGerund == "ing"):
string = "to " + string[0:stringLength - 3]
else:
string = "That's not a gerund!"
return string
# Add more statements to test what your function does:
print (gerund_infinitive("building"))
print (gerund_infinitive("build"))


Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux to build That's not a gerund!


Reference site : https://hourofpython.trinket.io/python-challenges#/string-challenges/gerund-slicing-challenge

반응형