Here is a basic example def signx if x 0 return

This preview shows page 3 - 6 out of 33 pages.

Here is a basic example. def sign(x): if x > 0: return ' Positive ' else: return ' Negative ' If the input x is greater than 0 , 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 which the conditional expression is true will be evaluated. Each if and elif expression is evaluated and considered in order, starting at the top. As soon as a true value is found, the corresponding body is executed, and the rest of the conditional statement is skipped. If none of the if or elif expressions are true, then the else body is executed. For more examples and explanation, refer to the section on conditional statements here . Question 2. Complete the following conditional statement so that the string ' More please ' is assigned to the variable say_please if the number of nachos with cheese in ten_nachos is less than 5 . Hint : You should not have to directly reference the variable ten_nachos .
3
--------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed Question 3. Write a function called nacho_reaction that returns a string based on the type of nacho passed in as an argument. From top to bottom, the conditions should correspond to: ' cheese ' , ' salsa ' , ' both ' , ' neither ' . In [11]: def nacho_reaction (nacho): if nacho == ' cheese ' : return ' Cheesy! ' # next condition should return ' Spicy! ' elif nacho == ' salsa ' : return ' Spicy! ' # next condition should return ' Wow! ' elif nacho == ' both ' : return ' Wow! ' # next condition should return ' Meh. ' elif nacho == ' neither ' : return ' Meh. ' spicy_nacho = nacho_reaction( ' salsa ' ) spicy_nacho Out[11]: ' Spicy! ' In [12]: _ = ok . grade( ' q1_3 ' ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 1 Failed: 0 [ooooooooook] 100.0% passed Question 4. Add a column ' Reactions ' to the table ten_nachos_reactions that consists of reactions for each of the nachos in ten_nachos . Hint: Use the apply method.
4
Out[13]: Nachos | Reactions neither | Meh. cheese | Cheesy! both | Wow! both | Wow! cheese | Cheesy! salsa | Spicy! both | Wow! neither | Meh. cheese | Cheesy! both | Wow! In [14]: _ = ok . grade( ' q1_4 ' ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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 . In [15]: number_wow_reactions = np . count_nonzero(ten_nachos_reactions . column( ' Reactions ' ) == ' W number_wow_reactions Out[15]: 4 In [16]: _ = ok . grade( ' q1_5 ' ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running tests --------------------------------------------------------------------- Test summary Passed: 2 Failed: 0 [ooooooooook] 100.0% passed 1.2 2. Simulations and For Loops Using a for statement, we can perform a task multiple times. This is known as iteration. Here, we’ll simulate drawing different suits from a deck of cards.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture