Dec-26-2018, 02:14 PM
(This post was last modified: Dec-26-2018, 10:43 PM by Drone4four.)
Here is the homework task:
Here is the solution that I came up with:
Hooray!
However I wanted to take this script one step further by adding proper punctuation so that “ready” is changed to “Ready”. And “home” is adjusted so that it reads “Ready”. I have set out to achieve this by invoking both the lower and capitalize casting methods. My new script looks like this:
At this point the course-task is complete so I am ready for the full answer from someone here.
For my future reference, the course material can be found here: Pierian-Data/Complete-Python-3-Bootcamp. The specific sub- module I am working on is: 03-Methods and Functions/03-Function Practice Exercises.ipynb
EDIT: I made some further progress:
Rather than letting the casting methods sit there, I just assigned them to a new variable. Here is what my script looks like now:
Quote:MASTER YODA: Given a sentence, return a sentence with the words reversed¶
master_yoda('I am home') --> 'home am I'
master_yoda('We are ready') --> 'ready are We'
Note: The .join() method may be useful here. The .join() method allows you to join together strings in a list with some connector string. For example, some uses of the .join() method:
>>> "--".join(['a','b','c'])
>>> 'a--b--c'
This means if you had a list of words you wanted to turn back into a sentence, you could just join them with a single space string:
>>> " ".join(['Hello','world'])
>>> "Hello world"
Here is the solution that I came up with:
def master_yoda(text): string = text.split() string.reverse() newstring = ' '.join(string) return newstringThis produces the expected output.;
Quote:master_yoda('I am home') --> 'home am I'
master_yoda('We are ready') --> 'ready are We'
Hooray!
However I wanted to take this script one step further by adding proper punctuation so that “ready” is changed to “Ready”. And “home” is adjusted so that it reads “Ready”. I have set out to achieve this by invoking both the lower and capitalize casting methods. My new script looks like this:
def master_yoda(text): string = text.split() string.reverse() newstring = ' '.join(string) newstring[0:-1].lower() # Casting the entire string to be lowercase newstring[0].capitalize() # Casting the first letter to be uppercase return newstringBut the output is the same as before. “ready” and “home” still remain uncapitalized. What is going on at lines 5 and 6? What would you people recommend I use instead?
At this point the course-task is complete so I am ready for the full answer from someone here.
For my future reference, the course material can be found here: Pierian-Data/Complete-Python-3-Bootcamp. The specific sub- module I am working on is: 03-Methods and Functions/03-Function Practice Exercises.ipynb
EDIT: I made some further progress:
Rather than letting the casting methods sit there, I just assigned them to a new variable. Here is what my script looks like now:
def master_yoda(text): string = text.split() string.reverse() newstring = ' '.join(string) lowered_string = newstring[0:-1].lower() # Casting the entire line to be lowercase final_string = lowered_string[0].capitalize() # Casting the first letter to be uppercase return final_stringWith master_yoda('I am home') I am still expecting “Home am i” as the output but now all I am getting is ‘H’. Likewise, with master_yoda('We are ready') I am also expecting “Ready are we” yet I am getting just: “R”. Why?