In 114 the unrolled version of this for loop can be

This preview shows page 5 - 8 out of 12 pages.

In [114]:The unrolled version of thisforloop can be found below.In [115]:In the example above, theforloop appends a random draw to thedrawsarray for everynumber innp.arange(repetitions).Here's a nice way to think of what we did above. We had a deck of 4 cards of different suits, werandomly drew one card, saw the suit, kept track of it indraws, and put the card back into thedeck. We repeated this for a total of 6 times without having to repeat code, thanks to theforOut[112]:4Out[113]:All tests passed!Out[114]:array(['', '', '', '', '', ''], dtype='<U32')Out[115]:array(['', '', '', '', '', ''], dtype='<U32')number_wow_reactions=ten_nachos_reactions.where('Reactions', are.equal_to('Wow!number_wow_reactionscheck('tests/q1_5.py')suits=make_array("","","","")draws=make_array()repetitions=6foriinnp.arange(repetitions):draws=np.append(draws, np.random.choice(suits))drawsdraws=make_array()draws=np.append(draws, np.random.choice(suits))draws=np.append(draws, np.random.choice(suits))draws=np.append(draws, np.random.choice(suits))draws=np.append(draws, np.random.choice(suits))draws=np.append(draws, np.random.choice(suits))draws=np.append(draws, np.random.choice(suits))draws
4/4/2021lab06 - Jupyter Notebook6/12loop. We simulated this experiment using aforloop.Another use of iteration is to loop through a set of values. For instance, we can print out all of thecolors of the rainbow.In [116]:We can see that the indented part of theforloop, known as the body, is executed once for eachitem inrainbow. Note that the namecoloris arbitrary; we could easily have named itsomething else. The important thing is we stay consistent throughout the for loop.In [117]:In general, however, we would like the variable name to be somewhat informative.Question 2.1Clay is playing darts. His dartboard contains ten equal-sized zones with point values from 1 to 10.Write code that simulates his total score after 1000 dart tosses. Make sure to use aforloop.Hint:There are three steps to this problem (and most simulations):1. Deciding the possible values you can take in the experiment (point values in this case)2. Running through the experiment a certain amount of times (running through 1000 dart tosses,and randomly getting a value per toss in this case)3. Keeping track of the total information of each time you ran through the experiment (the totalscore in this case)redorangeyellowgreenblueindigovioletredorangeyellowgreenblueindigovioletrainbow=make_array("red","orange","yellow","green","blue","indigo","violeforcolorinrainbow:print(color)foranother_nameinrainbow:print(another_name)
4/4/2021lab06 - Jupyter Notebook7/12In [118]:In [119]:Question 2.2In the following cell, we've loaded the text ofPride and Prejudiceby Jane Austen, split it intoindividual words, and stored these words in an array. Using aforloop, assignlonger_than_five

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 12 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
NoProfessor
Tags
Pride and Prejudice, Nachos, elif nacho

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture