We've been asked to write a program that takes in a message and triples the last letter of each (essentially) word. Here's a sample user interaction:
What is your message? I love spring!
III loveee spring!!!
How will we create our program? In this walkthrough, we'll emphasize the design and implementation phases of developing a program. While this program might feel straightforward enough to write immediately, the purpose of the walkthrough is to emphasize good practice for this process!
This is similar to what you might do when writing a paper. If you're asked to write a 50 page paper, you need to come up with your overall argument, then move to an outline that organizes ("designs") the content before expanding ("implementing") each section. When you expand your sections, it doesn't matter the order you go, as you know you have a placeholder for each spot. For a short essay, perhaps you skip the outlining process, but this shortcut can sometimes lead you astray!
This is often given to us, but it helps to spend time understanding what is being asked of us. We might work an example, like this:
We'll be writing a program that takes user input, such as "Hello there."
We'll then create a new version of their message that triples the last character of each word (or punctuation mark) and prints it out, as in "Hellooo there..."
During this phase, we come up with a design for how to solve this problem.
Your design should help identify the steps involved in solving the problem
These steps typically become our functions
Step 1: Ask the user for input
This won't require any information (parameters)
It should return a str
Prompt the user for their message and return the result
Step 2: Triple the last letter of each word in a message
As input/parameter, take in a str that is the message
Return a str
Given a message, return a new version with the last letter of each word tripled ("Hello there." -> "Hellooo there...")
Step 3: To help with step 2, triple the last letter of a given word
As input/parameter, take in a str called word
Return a str
Given a word, return a new version with the last letter tripled ("Hello" -> "Hellooo")
Use the design to create a "skeleton" file that has stubs for each function. This gives us a framework for developing our code where we can focus on each function separately.
Each function's "stub" should have a body that returns a "placeholder" value consistent with the return type.
Choose thoughtful values that will remind you that you still have work to do!
An int return type could have a placeholder of -1
A bool return type could have a placeholder of False
A str return type could have a placeholder of ""
A list return type could have a placeholder of []
A None return type could print( "TBD!" )
def getUserInput() -> str:
"""Prompt the user for their message and return the result"""
return "Hello there."
def tripleMessage( msg : str ) -> str:
"""Given a message, return a new version with the last letter of each word tripled
("Hello there." -> "Hellooo there...") """
return msg
def tripleWord( word:str ) -> str:
"""Given a word, return a new version with the last letter tripled ("Hello" -> "Hellooo")"""
return word
def main():
userMsg:str = getUserInput()
newMsg:str = tripleMessage(userMsg)
print(newMsg)
if __name__ == "__main__":
main()
def getUserInput() -> str:
"""Prompt the user for their message and return the result"""
userMsg:str = input( "What is your message? " )
return userMsg
def tripleMessage( msg : str ) -> str:
"""Given a message, return a new version with the last letter of each word tripled
("Hello there." -> "Hellooo there...") """
return msg
def tripleWord( word:str ) -> str:
"""Given a word, return a new version with the last letter tripled ("Hello" -> "Hellooo")"""
lastLetter:str = word[len(word)-1]
return word + lastLetter*2
def test():
testWord:str = "hello"
print( tripleWord(testWord) )
def main():
userMsg:str = getUserInput()
newMsg:str = tripleMessage(userMsg)
print(newMsg)
if __name__ == "__main__":
# main()
test()
def getUserInput() -> str:
"""Prompt the user for their message and return the result"""
userMsg:str = input( "What is your message? " )
return userMsg
def tripleMessage( msg : str ) -> str:
"""Given a message, return a new version with the last letter of each word tripled
("Hello there." -> "Hellooo there...") """
words:list = msg.split()
newMsg:str = ""
for word in words:
print( word )
newMsg += tripleWord(word) + " "
return newMsg
def tripleWord( word:str ) -> str:
"""Given a word, return a new version with the last letter tripled ("Hello" -> "Hellooo")"""
lastLetter:str = word[len(word)-1]
return word + lastLetter*2
def test():
testWord:str = "hello"
print( tripleWord(testWord) )
def main():
userMsg:str = getUserInput()
newMsg:str = tripleMessage(userMsg)
print(newMsg)
if __name__ == "__main__":
# main()
test()