Python | Comprehensions exercise | Analyse a speech from Julius Caesar
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Open the Python program in the above folder and run it - you should see a speech from Julius Caesar:
Will Shakespeare - master of the metaphor.
Analyse this speech to see the word count as follows:
Some thoughts on how you could go about doing this are shown below!
Here are some ideas for how to proceed:
|Getting a list of words||Use the split function to split your text, using a space character as a delimiter.|
|Removing punctuation||Some words contain , and . characters, and some contain \n line breaks. Loop over the list of words, applying the replace function to each to remove these extra characters. You should also use the lower function to convert each word to lower case, to make sure that And is counted with and, for example.|
|Producing the output||Loop over all the numbers in a range from 1 to (say) 20. For each number, use a list comprehension to find how many words there are with this many letters in (see below), and apply an if statement to print out a message if the number of words is more than zero.|
Here is some suggested text for your list comprehension, to give you the idea of the syntax:
# show number of words with this many letters
# (this uses a list comprehension)
number_words = len([x for x in words if len(x) == num])
There are probably better and definitely other ways to solve this; feel free to do your own thing (the result is what counts ...).
When you've got this working, save your program as Speech impediment.py and close it down.