Flag Variables | BUS 104 – Python Exam

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.

Considering that flag variables are boolean, they must be true or false.

So the real question is... why not assign correctly_answered as "True" to start the program?

Because if we do so, the while loop will never run. Notice the condition of the while loop?

correctly_answered = False

while correctly_answered == False:
    user_answer = int(input("What is 5 x 9?"))
    if user_answer == 45:
        ...

The while loop only runs when correctly_answered is "False". So, if we start the program with correctly_answered as "True", the while loop will never run!

If you're still confused, think about it like this: has the user correctly answered what "5 x 9" is before the program even starts? No! So correctly_answered should not equal "True", it should equal "False".

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:

  • First, the user enters 12, which is an incorrect answer to the question.

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!

ToolsPython Follow-along Guide (FREE)
LessonPrint function
LessonInput function
LessonConcatenation
LessonConditionals (if, elif, else)
LessonWhile loops
LessonFor loops
LessonData Types
LessonNested Conditionals
LessonFlag Variables

Practice Problems

Include step-by-step explanations through each part of the problem!

Python Cram Kit Logo

"...I was blown away with the videos and the ability to explain what is going on at a simple level. Highly recommend!"

Jack Whelan
March 8, 2021

Rating: 5 out of 5.

Leave a Comment