CS 111 f21 — Functions and Conditionals part 2

Table of Contents

1 Lab 1 questions

  • You will be adding functions (strategies) to prisoner2.py and prisoner3.py where it says

    # DEFINE STRATEGIES HERE
    
  • All of these functions have as parameters the history of the current player (my_history) and the opponent (other_history).
  • These history objects have functions associated with them (listed in the History documentation)
    • Functions associated with particular objects are called methods
    • We can use these functions to ask a particular history for information
    • For example,

      my_history.get_length()
      

      returns the number of actions in my_history. How could we use that in an if statement to check if it is the first round of the game?1

    • As the lab writeup says, Python automatically fills in the self parameters for us. We will learn more about self later, but for now we can just ignore it.
  • How would we find out the number of times our opponent has cooperated?2

2 Review

  • In what order will these lines be executed3

    1: cur_temp = 28
    2: def C_to_F(temp_C):
    3:     temp_F = temp_C * 9 / 5 + 32
    4:     return temp_F
    5: cur_temp_F = C_to_F(cur_temp)
    6: print(cur_temp_F)
    

3 Functions

  • Why might we want to use functions?
  • Possible errors
    • Wrong number of inputs
    • Unindenting something that should be part of the function

4 Practice

  • Write an average_grade function that takes three numeric grades as parameters and returns their average4
  • Function mysteries
    • What will be printed?5

      def     f(x):
          return x + 2
      def     g(y):
          return y * 3
      a = 2
      print(f(g(a)), g(f(a)))
      
    • What will be printed?6

      def     f(x):
          return x + 2
      def     g(y):
          return f(y) * f(y + 1)
      a = 2
      print(g(a))
      

5 Conditionals

  • Since the start of the term we've been talking about having a computer make a decision based on some condition (e.g., decide what weather icon to display)
  • We can do this with Python's if

    if CONDITION:
        print("this is only printed when CONDITION is True")
    print("this is always printed")
    
  • We can use if-else to cause the computer to take one of two different paths

    if CONDITION:
        print("this is only printed when CONDITION is True")
    else:
        print("this is only printed when CONDITION is False")
    print("this is always printed")
    
  • What if we want the text to have more than settings (normal, yellow, red)?
    • Could do something like

      if cur_temp > hot_temp_C:
          # print in red
      else:
          if cur_temp > hot_temp_C - 5:
             # print in yellow
          else:
             # print normally
      

      but this would quickly get cumbersome with tons of indenting as the number of different outcomes grew

  • Instead, we can use if-elif-else

    if cur_temp > hot_temp_C:
        # print in red
    elif cur_temp > hot_temp_C - 5:
        # print in yellow
    else:
        # print normally
    
  • What if we had multiple conditions?

    if cur_temp > hot_temp_C:
        if chance_of_rain > 0.4:
            # display text in red with cloudy icon
        elif chance_of_clouds > 0.5:
            # display text in red with cloudy icon
    else:
       # ...
    
    • We can use and and or to combine several Boolean expressions into a single condition

      if cur_temp > hot_temp_C and (chance_of_rain > 0.4 or chance_of_clouds > 0.5):
          # display text in red with cloudy icon
      else:
          # ...
      
      • A and B will be True when both A and B are True
      • A or B will be True when either A or B is True
        • Also when both A and B are True

6 Practice

  • Write an absolute value function: takes one number as a parameter, returns the positive version of that number (hint: if x is negative, -x is the positive version).7
  • Mystery
    • What will be printed?8

      def     is_even(x):
          return x % 2 == 0
      turns = 7
      if turns == 0:
          print("WELCOME")
      elif is_even(turns):
          print("FOR FUN")
      else:
          print("IT'S TIME")
      
  • Given a player's guess and a secret number, print "cold" "warm" or "lava" (or "WINNER" if the guess is correct)9
    • Assume you have a variable guess with the player's guess and a variable secret with the secret number
    • Up to you to choose how close "lava" is, etc.
    • Make sure to only print one hint

Footnotes:

1
if my_history.get_lenth() == 0:
2

Ask other_history!

opponent_cooperations = other_history.get_num_coops()
3

1,2,5,3,4,6

4
def average_grade(grade1, grade2, grade3):
    return (grade1 + grade2 + grade3) / 3
5

8 12

6

20

7
def abs(x):
    if x < 0:
        return -x
    else:
        return x
8

"IT'S TIME"

9
difference = abs(secret - guess)
if difference == 0:
    print("WINNER")
elif difference < 5:
    print("lava")
elif difference < 10:
    print("warm")
else:
    print("cold")