Question 4 create a table tennachosreactions that

This preview shows page 5 - 9 out of 13 pages.

Question 4. Create a table ten_nachos_reactions that consists of the nachos in ten_nachos as well as the reactions for each of those nachos. The columns should be called Nachos and Reactions . Hint: Use the apply method.
5
ten_nachos_reactions = Table() . with_column( 'Nachos' , ten_nachos) . , with_column( 'Reactions' , ten_reactions) ten_nachos_reactions [40]: Nachos | Reactions neither | Meh. cheese | Cheesy! both | Wow! both | Wow! cheese | Cheesy! salsa | Spicy! both | Wow! neither | Meh. cheese | Cheesy! both | Wow! [41]: ok . grade( "q14" ); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed Question 5. Using code, find the number of ’Wow!’ reactions for the nachos in ten_nachos_reactions . [65]: number_wow_reactions = ten_nachos_reactions . where( "Reactions" , 'Wow!' ) . , select( "Reactions" ) . num_rows number_wow_reactions [65]: 4 [66]: ok . grade( "q15" ); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 2 Failed: 0 [ooooooooook] 100.0% passed 6
1.2 2. Simulations and For Loops Using a for statement, we can perform a task multiple times. This is known as iteration. One use of iteration is to loop through a set of values. For instance, we can print out all of the colors of the rainbow. [67]: rainbow = make_array( "red" , "orange" , "yellow" , "green" , "blue" , "indigo" , , "violet" ) for color in rainbow: print (color) red orange yellow green blue indigo violet We can see that the indented part of the for loop, known as the body, is executed once for each item in rainbow . Note that the name color is arbitrary; we could easily have named it something else. The important thing is we stay consistent throughout the for loop. [68]: for another_name in rainbow: print (another_name) red orange yellow green blue indigo violet In general, however, we would like the variable name to be somewhat informative. Question 1. In the following cell, we’ve loaded the text of Pride and Prejudice by Jane Austen, split it into individual words, and stored these words in an array p_and_p_words . Using a for loop, assign longer_than_five to the number of words in the novel that are more than 5 letters long. Hint : You can find the number of letters in a word with the len function. [99]: austen_string = open ( 'Austen_PrideAndPrejudice.txt' , encoding = 'utf-8' ) . read() p_and_p_words = np . array(austen_string . split()) longer_than_five = 0 for word in p_and_p_words: if len (word) > 5 : 7
longer_than_five = longer_than_five + 1 # a for loop would be useful here print (longer_than_five) 35453 [100]: ok . grade( "q21" ); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture