# Control Flow
So far, every Python program that we’ve encountered has only had one path of execution — they all execute one line at a time, from top to bottom. And every time you run them, it gives you the same exact result.
Sometimes, we want our program to do different things based on different conditions.
It's like going down a highway and there's a fork in the road up ahead with a sign that reads "San Francisco: left. Los Angeles: right."
In the "Control Flow" chapter, we will explore how programs "make decisions" by evaluating different conditions. And start introducing logic into our code!
# Instructions
Before we dive deep into something called an if
statement, let's do a demo using a coin flip simulation!
Create a coin_flip.py program and type in the following:
import random
num = random.randint(0, 1) # RNGesus will give us a random number between 0 and 1
if num > 0.5:
print("Heads")
else:
print("Tails")
All you need to know is that this program simulates a coin toss:
- ≈ 50% of the time, it's "Heads".
- ≈ 50% of the time, it's "Tails".
Run the program 5 times to get a taste of the if
/else
statement!
How many times did it go Heads?
Hint
In this challenge, you don't have to know what's going on with the code. You can just copy and paste.
Back