And we’re back! Let another day of Python begin!! We know how to make variables (Strings and Numbers) and we know how to make functions that do stuff to those variables. But what if we want to do something different for each kind of variable we get? Well, the answer lies within one of the words in the previous sentence.

I wonder IF you’ll get it…

def hello(foo="hello",bar="world"):
  if bar is not "world":
    print("Who are you saying hello to? oh hi "+bar)
  else:
    print(foo+" "+bar)

Try this function out in the following ways:

hello() #prints 'hello world' 
hello("someone") #prints 'someone world'
hello("hello","Jacob") #prints "Who are you saying hello to? oh hi Jacob"
hello(bar="worlds") #prints "Who are you saying hello to? oh hi worlds"
hello(foo="goodbye") #prints 'goodbye world'

So, in python we use the keyword if to begin a question and we use the following words to ask the question:

In addition to if, we also have:

Let’s see this in practice:

secret_number = 27
lower_bound = 0
upper_bound = 100


def guessing_game(number):
    if number <= lower_bound or number >= upper_bound:
        print("Out of bounds!")
        return  # stops the method
    if number > secret_number:
        print("TOO HIGH!")
    elif number < secret_number:
        print("TOO LOW!")
    elif number == secret_number:
        print("You got it! My number was {0}".format(secret_number))
    else:
        print("This should never happen!")

guessing_game(101)
guessing_game(-1)
guessing_game(50)
guessing_game(30)
guessing_game(20)
guessing_game(27)

Something to note: In my code I used a function called format. This function allows us to print strings and numbers together. Format looks inside of a string for curly brackets with numbers (starting at 0). It then takes the first parameter in the function and puts it in the right place (more on this later)

Lets make this a bit cooler though, it’s kinda of annoying that in order to play the game you have to program, lets add some user input! We’re going to use a simple built-in function called input

secret_number = 27
lower_bound = 0
upper_bound = 100
their_number = -100


def guessing_game(number):
    if number <= lower_bound or number >= upper_bound:
        print("Out of bounds!")
        return  # stops the method, we could also just say return, but break is a really useful control flow statement
    if number > secret_number:
        print("TOO HIGH!")
    elif number < secret_number:
        print("TOO LOW!")
    elif number == secret_number:
        print("You got it! My number was {0}".format(secret_number))
    else:
        print("This should never happen!")


while their_number is not secret_number:
    their_number = input("Guess a number: ")
    guessing_game(their_number)

So this is the whole program. Lets go over what the new stuff does:

Well that’s all for now, next time we’re gonna go over lists and some more powerful python syntax.