Shakespeare: Difference between revisions

imported>Zanwenhuahao
imported>Zanwenhuahao
Line 144:
For more information on dictionary: https://docs.python.org/2/tutorial/datastructures.html#dictionaries
====Lists and Dictionaries Exercises====
List & Iteration Exercise 1:
* Create a new python program.
* Import the list of characters from <b> A Midsummer Night's Dream </b> saved in the file <code>AMsND_Char.txt</code>
* Save it to a list, where each item in the list should be the name of the character enclosed in angled brackets, e.g. ['<HELENA>', ...]
* Print the list to see that you got it right
* For good practice, you should close the file by using
<pre>
>>> myFile = open("name_of_file", "r")
...
>>> myFile.close()
</pre>
 
List & Iteration Exercise 2:
* In the same program that you created from exercise 1
* Now open the play <b> A Midsummer Night's Dream </b>
* Read through the play line by line, count how many times has <b> <OBERON> </b> spoke.
* Print to screen the name <OBERON> and the number of times he spoke.
* Close the file.
 
List & Iteration Exercise 3*:
* Use the same program as above
* Open the same play again
* Now iterate through the list of character you saved from exercise 1, see how many times each of them speak.
* Print to screen each character's name and the number of times they spoke.
* Close the file.
 
* <b>Note:</b> You can only readline() through a file once, so for each line read, you must check that if any of the characters have started a new speech that line.
 
List & Iteration Exercise 4**:
* Do the same as exercise 3, except now instead of printing them directly, save the <b>names of character</b> and <b>number of times they spoke</b> as a <b>key:value</b> pair in to a dictionary.
* Print the dictionary to screen and check that you have the same result as part 3.
Anonymous user