반응형
# 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
반응형
'<개인공부> - IT > [Python]' 카테고리의 다른 글
Number of Things Challenge 함수 만들기 (0) | 2018.08.20 |
---|---|
Commafy 함수 만들기 (0) | 2018.08.20 |
Reverse String 함수 만들기 (0) | 2018.08.20 |
isupper(), islower() 함수이용하여 문자열의 대소문자 변경하기 (0) | 2018.08.19 |
Python 소문자 변경하는 함수만들기 (0) | 2018.08.17 |