1 / 17

Q and A for Section 4.4

Q and A for Section 4.4. CS 106, Fall 2014. Q1. Q: The syntax for an if statement (or conditional statement ) is: if _______________ : _____________ A: if <boolean expression>: <body>. Q2.

reba
Download Presentation

Q and A for Section 4.4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Q and A for Section 4.4 CS 106, Fall 2014

  2. Q1 Q: The syntax for an if statement (or conditional statement) is: if _______________ : _____________ A: if <boolean expression>: <body>

  3. Q2 Q: Write code that prints out "Brilliant!" if the value in variable food is "spam". A: if food == "spam": print "Brilliant!"

  4. Q3 Q: What happens if the value of food is "bubble and squeak"? A: Nothing.

  5. Q4 Q: Write code to print "Brilliant!" if food is "spam" and how_preparedis "on toast". A: if food == "spam" and how_prepared == "on toast": print "Brilliant!" (or...) What if we don't like it on toast?

  6. Q5 Q: Write code to print "Brilliant!" if food is "spam" and how_prepared is "on toast", and to print "Awesome!" if how_prepared is "in a casserole". A: if food == "spam": if how_prepared == "on toast": print "Brilliant!" if how_prepared == "in a casserole": print "Awesome!"

  7. Q6 Q: An if-else statement has this format: if _____________ : ___________ else: ___________ A: <boolean expression>; <if body>; <else body>

  8. Q7 Q: Suppose you have a function is_prime(x) that returns True if x is a prime number. Write code to add xto a list primes if xis prime and to a list non_primesotherwise. A: if is_prime(x): primes.append(x) else: non_primes.append(x)

  9. Q8 Q: Write code that prints "NaN" if the list nums is empty or, otherwise, computes and prints the average of the list of floats. A: if len(nums) == 0: print "NaN" else: print sum(nums) / len(nums)

  10. Non-booleans used as booleans Q: In the “for the guru” on page 142, there is code if not dinner:. Can you explain that? A: Why, yes. Yes, I can. The pattern is if <boolean expression>: So, if the expression given is not a boolean (dinner is a string), then it is converted to a boolean, as if bool(dinner) were called. In general, if a value is empty (“” or []), the value is False; else, True.

  11. Q9 Q: Write code that prints "Let's go" if the variable response equals either 'y' or 'yes'. A: if response in ('y', 'yes'): print "Let's go" (not if response == 'y' or 'yes' !!)

  12. Q10 Q: You are given a long string gen containing only As, Gs, Ts, and Cs. Write code to count the number of As and the number of Gs in the string. countAs = 0 countGs = 0 for ch in gen: if ch == 'A': countAs += 1 elif ch == 'G': countGs += 1

  13. Q11 Q: You have 2 operands, op1 and op2, and an operator, op, that is one of '+', '-', '*', '/'. Write code to compute the correct result into res. A: if op == '+': res = op1 + op2 elif op == '-': res = op1 - op2 elif op == '*': res = op1 * op2 elif op == '/': res = op1 / op2

  14. Q12 Q: What can we do to the previous code to print out "Oops!" if op contains a bad value? A: Add else: print 'Oops!'

  15. Q13 Q: Rewrite this code using if-elif: if i < 7: do_something(i) else: if j > 9: do_something_else(j) A: if i < 7: do_something(i) elifj > 9: do_something_else(j)

  16. Nested if vs. elif Q: When would you prefer an if nested inside an else, over using an elif. A: An if-else is a binary decision: there are two choices. An if-elif-elif-else is an n-way decision: some variable can belong to n different “groups” or “classes”. If you are comparing a variable n to something, and then in one of the results have to “classify” some other variable, use a nested if.

  17. Q14 Q: Write code to print out a student's letter grade, based on the value in score. Pick your own scale. A: if score > 92: print 'A' elif score > 85: print 'B' elif score > 77: print 'C' elif score > 70: print 'D' else: print 'F'

More Related