Still feeling nervous for your exam? Check out the Python Cram Kit for practice questions that emulate the difficulty that you'll face on your exam. Each question includes a thorough step-by-step explanation where I walk you through the entire problem solving process!
Flag variables explained
Remember practicing multiplication tables as a kid and your teacher repeatedly asking you what "5 x 9" was until you got it right?

In this situation, the flag variable was whether or not you got the problem correct.
If you answered the problem incorrectly, the teacher would continue the loop of asking you the question.
However, if you answered the problem correctly, the teacher end the loop and stop asking you the question.
In other words...
A flag variable is a boolean value that signals to a while loop whether or not to continue running.
Coding a flag variable
Let's take the situation above and apply it in Python terms to get a better idea of how it works. I've coded the majority of the program, that way we can focus on how the flag variable works.
Make sure to open a Trinket file to follow-along! (How to create a Trinket file?)
correctly_answered = False
while correctly_answered == False:
user_answer = int(input("What is 5 x 9?"))
if user_answer == 45:
...
Need help understanding how to code a while loop like the one above? Read While loops | Python in 30 Minutes!
Let's walk through this program and understand how it works, then get to incorporating the flag variable at the end!
Defining our flag variable
First, we must define our flag variable as a boolean value. Need a reminder of what a boolean value is?
Reminder: A boolean value is one that is either true or false.
correctly_answered = False
while correctly_answered == False:
user_answer = int(input("What is 5 x 9?"))
if user_answer == 45:
...
Here, we're correctly_answered as our flag variable and giving it a value of "False" to start.
Coding the rest of the while loop
correctly_answered = False
while correctly_answered == False:
user_answer = int(input("What is 5 x 9?"))
if user_answer == 45:
...
Here, we are determining whether or not we run the loop. If the loop runs (a.k.a. the user has not correctly answered the question yet), keep asking the user what "5 x 9" equals!
correctly_answered = False
while correctly_answered == False:
user_answer = int(input("What is 5 x 9?"))
if user_answer == 45:
...
Now for the important part:
correctly_answered = False
while correctly_answered == False:
user_answer = int(input("What is 5 x 9?"))
if user_answer == 45:
...
Here, we are actually figuring out whether or not the user correctly answered what "5 x 9" equals.
If the user_answer...
correctly_answered = False
while correctly_answered == False:
user_answer = int(input("What is 5 x 9?"))
if user_answer == 45:
...
...equals 45...
correctly_answered = False
while correctly_answered == False:
user_answer = int(input("What is 5 x 9?"))
if user_answer == 45:
...
...we need execute whatever's within the conditional statement:
correctly_answered = False
while correctly_answered == False:
user_answer = int(input("What is 5 x 9?"))
if user_answer == 45:
...
Alright, it's game time... what should we put in this conditional statement to effectively utilize flag variables?
Updating our flag variable
What you should be asking here is: what would cause the loop to stop running? We want the loop to stop running once they answer the question correctly.
Look back at the condition of our while loop here to figure this out.
correctly_answered = False
while correctly_answered == False:
user_answer = int(input("What is 5 x 9?"))
if user_answer == 45:
...
We must switch the value of correctly_answered from "False" to "True", like so!
correctly_answered = False
while correctly_answered == False:
user_answer = int(input("What is 5 x 9?"))
if user_answer == 45:
correctly_answered = True
That way, when the loop tries to run again, correctly_answered won't equal "False" anymore, and the loop will stop running! (Therefore, it'll stop asking the user what "5 x 9" equals.)
Type the above code into your Trinket file and give it a shot! You should get the following output:
Python Follow-along Guide
It's no secret you retain info better when you write it down. That's why I created the Python Follow-along Guide for you!

As you come upon key concepts highlighted in yellow, like this sentence here, you can fill-in-the-blanks on your Python Follow-along Guide so that you remember all the important stuff for later!
You can obtain your Python Follow-along Guide for FREE by entering your email below!
Congratulations! You've gone through all the core concepts that will be on your Python exam! Now what?
Practice, practice, practice.
I cannot emphasize enough how much more important it is to practice Python rather than study it. Without practice, it's difficult to truly understand what you're coding when it comes time for your exam.
In the Python Cram Kit, I've compiled practice questions that emulate the difficulty that you'll face on your exam. Each question includes a thorough step-by-step explanation where I walk you through the entire problem solving process. That way, you never feel lost, and are prepared to solve any exam problems thrown your way! Click below to get started!
Concepts
Each exam concept broken down with relatable situations to your life!
![]() | Tools | Python Follow-along Guide (FREE) |
![]() | Lesson | Print function |
![]() | Lesson | Input function |
![]() | Lesson | Concatenation |
![]() | Lesson | Conditionals (if, elif, else) |
![]() | Lesson | While loops |
![]() | Lesson | For loops |
![]() | Lesson | Data Types |
![]() | Lesson | Nested Conditionals |
![]() | Lesson | Flag Variables |

I’m a Miami University (OH) 2021 alumni who majored in Information Systems. At Miami, I tutored students in Python, SQL, JavaScript, and HTML for 2+ years. I’m a huge fantasy football fan, Marvel nerd, and love hanging out with my friends here in Chicago where I currently reside.