# 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?)

user_answer = int(input("What is 5 x 9?"))
...

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.

user_answer = int(input("What is 5 x 9?"))
...

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?

user_answer = int(input("What is 5 x 9?"))
...

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

user_answer = int(input("What is 5 x 9?"))
...

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!

user_answer = int(input("What is 5 x 9?"))
...

Now for the important part:

user_answer = int(input("What is 5 x 9?"))
...

Here, we are actually figuring out whether or not the user correctly answered what "5 x 9" equals.

user_answer = int(input("What is 5 x 9?"))
...

...equals 45...

user_answer = int(input("What is 5 x 9?"))
...

...we need execute whatever's within the conditional statement:

user_answer = int(input("What is 5 x 9?"))
...

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.

user_answer = int(input("What is 5 x 9?"))
...

We must switch the value of correctly_answered from "False" to "True", like so!

user_answer = int(input("What is 5 x 9?"))

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!

### Practice Problems

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

"...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.