When we were first developing our game, I introduced this If function. In this lesson, I will talk to you about how it works.
To understand the If function properly, we first need to understand the Boolean data type. A Boolean can be either True or False. We can store their value in variables:
X = True
X = False
Try opening python and multiplying True times any number and False times any number (4 * True, 203 * False etc.). If you need a refresher on mathematical operations in python, you can find it here. Don’t have python? Learn how to download it here or run your code online here. Don’t forget to use print() if you are using the online editor!
Hopefully you noticed that True is also equal to 1 and False is also equal to 0.
In python (and pretty much all other programming languages), you can compare to quantities and get a Boolean. For example, if I want to check if two strings are equal, I could write:
str1 == str2
Try running this by making these strings the same or different. Again, don’t forget to use print(). It should return True and False respectively. You can also compare numbers. This is more useful as you can use > or < and much more. Some of the most common symbols are below:
|==||True if a is equal to b|
|!=||True if a is not equal to b|
|>||True if a is greater than b|
|<||True if a is less than b|
|>=||True if a is greater than or equal to b|
|<=||True it a is less than or equal to b|
Try out some of these in python. The first two will work for all data types but the others will only work for some. Now that we understand the Boolean data type, we can move on to learning about the If function
The If function
Now that we know about Booleans, we are ready to learn about the If function. The If function has similar syntax to the for loop. It looks like this:
Just like in the for loop, the code under the If function must be indented using the TAB key. The If function runs the code below it if and only if the Boolean is True. Try the following in python. Set a variable isTrue to True. Then type:
—-print(“this is true”)
Next, try setting the variable to False. It should now print nothing. But what if you want it to print “this is false”? We will learn how to do that with the else function
The Else function is run if the Boolean in the If function above it is false. For example, sticking to the example above, You could type:
—-print(“This is True”)
—-print(“This is False”)
In this simple example, these functions are all we’ll need, but what if we have a more complicated situation?
Let’s consider this case: we have a number, let’s say 52, and the user has to guess it. If they guess correctly, we print “Correct”, if they’re too high we print “Too high” and if they’re too low we print (you guessed it) “Too low”. Try implementing this yourself in python. To do this we need to use the elif function.
The elif function is a combination of the else function and the If function. Like the else function, it only runs if the If function above it is False. Just like the If function, it only runs if the Boolean in its brackets is True. Here is an example:
—-do something else
Now try to implement this yourself. It should look look like this (its okay if yours is a bit different):
Try running this and guessing different numbers. Did it work? It turns out this is exactly what we need in our game! Try to implement this in the game we developed last lesson. Don’t forget to covert guess to an int using:
guess = int(input(“Guess a number between 1 and 100 >>>”))
And make sure not to convert num to a string using str().
The Final Product
Below is what the final version of our game should look like. Remember, it’s okay if your version looks slightly different, as long as it works.
Great Job, you have now made a moderately hard game in python! If you feel confident in your coding skills, you can try entering this contest. See you in the next lesson!