Shakespeare: Difference between revisions

imported>Zanwenhuahao
imported>Zanwenhuahao
Line 146:
<b>List & Iteration Exercise 1:</b>
* Create a new python program.
* Import the list of characters from <b> A Midsummer Night's Dream </b> savedby inimporting <code>summerChar</code> and import the fileplay <code>AMsND_Char.txtsummerNight</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>
>>> summerChar
>>> myFile = open("name_of_file", "r")
# prints all the characters such as <HELENA> that are in the play A Midsummer Night's Dream
...
>>> myFile.close()
</pre>
 
<b>List & Iteration Exercise 2:</b>
* In the same program that you created from exercise 1 (or start a new file, but don't forget to import
* NowCount openhow themany playtimes has <b> A Midsummer Night's Dream<OBERON> </b> spoke.
* 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.
<b>Demo: Which of Shakespeare's famous tragic heros talks the most?</b>
 
<b>List & Iteration Exercise 3*:</b>
* 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.
 
<b>List & Iteration Exercise 4**:</b>
Anonymous user