# FAQ: Loops - While Loop

This community-built FAQ covers the “While Loop” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## FAQs on the exercise While Loop

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

var guess = Int.random(in: 1…10)
var magicNum = Int.random(in: 1…10)

while guess != magicNum {
guess = Int.random(in: 1…10)
magicNum = Int.random(in: 1…10)
print(“You guessed (guess), and the number was (magicNum).”)
}

The guided lessons says " * Reassign `guess` to `Int.random(in: 1...10)` .

• Also reassign `magicNum` to `Int.random(in: 1...10)` .

Note : if you don’t reassign `guess` , you may end up in an infinite loop. "

If the variables were assigned globally, why did they have to be restated in the while loop?

4 Likes

Bumping this up because I want to know, too.

1 Like

honestly was staring at the screen confused because I was like what do they mean reassign if they’re already defined.

1 Like

I don’t see the need for that line. The magic number should remain fixed, and only the guess would need to be changed in the loop.

1 Like

At the top of the code, we are declaring and assigning a value (a random integer between 1-10) to two variables `guess` and `magicNum`.
If we are really really lucky and both the numbers match, then we will skip the while loop and go to the print statement congratulating our correct guess.
However, if we we are not lucky and the condition `guess != magicNum` evaluates to true, then we will enter the while loop. If we leave both the variables untouched and don’t change any value, then we will be stuck in the while loop forever. Neither number is changed in every iteration of the loop and the condition `guess != magicNum` will keep evaluating to true.
We must change at least one of the numbers (reassigning guess makes sense), so that eventually we will randomly pick the same number as magicNum.