题目是在checkio上做的,这个网站有大量的python题界面类似wow:
For the input of your function will be given one sentence. You have to return its fixed copy in a way so it’s always starts with a capital letter and ends with a dot.
Pay attention to the fact that not all of the fixes is necessary. If a sentence already ends with a dot then adding another one will be a mistake.
Input: A string.
Output: A string.
Precondition: No leading and trailing spaces, text contains only spaces, a-z A-Z , and .
这边给出了一个代码框架,只需要在规定的地方填写好自己的代码,如果正常运行就过关啦。
def correct_sentence(text: str) -> str:
"""
returns a corrected sentence which starts with a capital letter
and ends with a dot.
"""
#your code
return text
if __name__ == '__main__':
print("Example:")
print(correct_sentence("Greetings, friends"))
# These "asserts" are used for self-checking and not for an auto-testing
assert correct_sentence("greetings, friends") == "Greetings, friends."
assert correct_sentence("Greetings, friends") == "Greetings, friends."
assert correct_sentence("Greetings, friends.") == "Greetings, friends."
assert correct_sentence("hi") == "Hi."
print("Coding complete? Click 'Check' to earn cool rewards!")
在your code处填写好自己的代码,然后试着运行,如果成功了,就说明代码正确啦。
附上我的代码:
return text.capitalize() + '.' * (not text.endswith('.'))