In 7 in 8 out7 3 running tests test summary passed 1

This preview shows page 3 - 8 out of 30 pages.

)
In [7]:In [8]:Out[7]:3~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Running tests---------------------------------------------------------------------Test summaryPassed: 1Failed: 0[ooooooooook] 100.0% passedten_nachos=make_array('neither','cheese','both','both','cheese','salsa','botnumber_cheese=np.count_nonzero(ten_nachos=='cheese')number_cheese_=ok.grade('q1_1')
Conditional StatementsA conditional statement is a multi-line statement that allows Python to choose among different alternativesbased on the truth value of an expression.Here is a basic example.def sign(x):if x > 0:return 'Positive'else:return 'Negative'If the inputxis greater than0, we return the string'Positive'. Otherwise, we return'Negative'If we want to test multiple conditions at once, we use the following general format.if <if expression>:<if body>elif <elif expression 0>:<elif body 0>elif <elif expression 1>:<elif body 1>...else:<else body>Only the body for the first conditional expression that is true will be evaluated. Eachifandelifexpression is evaluated and considered in order, starting at the top. As soon as a true value is found, thecorresponding body is executed, and the rest of the conditional statement is skipped. If none of theiforelifexpressions are true, then theelse bodyis executed.For more examples and explanation, refer to the section on conditional statementshere().Question 2.Complete the following conditional statement so that the string'More please'is assigned tothe variablesay_pleaseif the number of nachos with cheese inten_nachosis less than.5.Hint: You should be usingnumber_cheesefrom Question 1.
In [9]:In [10]:In [11]:Question 3.Write a function callednacho_reactionthat returns a string based on the type of nachopassed in as an argument. From top to bottom, the conditions should correspond to:'cheese','salsa''both','neither',.Hint:If you're failing the test, double check the spelling of your reactions.Out[9]:'More please'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Running tests---------------------------------------------------------------------Test summaryPassed: 1Failed: 0[ooooooooook] 100.0% passedsay_please='?'if...:say_please='More please'say_pleasesay_please='?'ifnumber_cheese<5:say_please='More please'_=ok.grade('q1_2')
In [12]:In [13]:In [14]:Out[12]:'Cheesy!'Out[13]:'Spicy!'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Running tests---------------------------------------------------------------------Test summaryPassed: 1Failed: 0[ooooooooook] 100.0% passeddefnacho_reaction(nacho):if...:return'Cheesy!'# next condition should return 'Spicy!'...# next condition should return 'Wow!'...# next condition should return 'Meh.'...spicy_nacho=nacho_reaction('salsa')spicy_nachodefnacho_reaction(nacho):ifnacho=='cheese':return'Cheesy!'elifnacho=='salsa':return'Spicy!'elifnacho=='both':return'Wow!'else:return'Meh.'spicy_nacho=nacho_reaction('salsa')spicy_nacho_=ok.grade('q1_3')

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 30 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags
Pride and Prejudice, Simple random sample

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture