Both pieces of code are correct, but they have slight differences in how they handle the completion message. Let me explain both and their differences:
Code 1: Using print Outside the Loop
# Initialize your score
score = 0
# While loop to keep playing the game until the score reaches 10
while score < 10:
    print(f"Your current score is {score}. Keep playing!")
    # Simulate earning a point
    score += 1  # This adds 1 to the score each time the loop runs
print("Congratulations! You reached a score of 10!")
Explanation
- The 
whileloop runs as long as the conditionscore < 10is true. - Inside the loop, it prints the current score and increments the score by 1.
 - Once the score reaches 10, the loop condition 
score < 10becomes false, and the loop stops. - The congratulatory message 
print("Congratulations! You reached a score of 10!")is outside the loop and executes after the loop has finished. 
Code 2: Using else with the while Loop
# Initialize your score
score = 0
# While loop to keep playing the game until the score reaches 10
while score < 10:
    print(f"Your current score is {score}. Keep playing!")
    # Simulate earning a point
    score += 1  # This adds 1 to the score each time the loop runs
else:
    print("Congratulations! You reached a score of 10!")
Explanation
- The 
whileloop runs as long as the conditionscore < 10is true. - Inside the loop, it prints the current score and increments the score by 1.
 - The 
elseblock associated with thewhileloop runs after the loop condition becomes false. - The congratulatory message 
print("Congratulations! You reached a score of 10!")is in theelseblock and executes after the loop has finished, just like in the first example. 
Differences and When to Use Which
- Code Structure:
 - Code 1: The completion message is outside the loop. This structure is straightforward and common in many programming scenarios.
 - Code 2: The completion message is inside the 
elseblock of the loop. This structure emphasizes that the message is part of the loop's normal termination process. - Usage of 
else: - Code 1: This is a conventional way to handle post-loop execution.
 - Code 2: Using 
elsewith thewhileloop can be useful for clarity, indicating that the code in theelseblock runs only if the loop terminates normally (without encountering abreakstatement). 
Example with break
Here’s an example where using else makes a difference:
# Initialize your score
score = 0
# While loop to keep playing the game until the score reaches 10
while score < 10:
    print(f"Your current score is {score}. Keep playing!")
    # Simulate earning a point
    score += 1  # This adds 1 to the score each time the loop runs
    if score == 5:
        print("An unexpected event occurred! Exiting the game early.")
        break
else:
    print("Congratulations! You reached a score of 10!")
print("Game Over")
Explanation
- The 
whileloop runs as long asscore < 10. - If the score reaches 5, the loop encounters a 
breakstatement, exiting the loop early. - Because of the 
break, theelseblock does not execute. - The message 
print("Congratulations! You reached a score of 10!")will not be printed if the loop is exited due tobreak. - The final message 
print("Game Over")will always be printed after the loop, regardless of how the loop is exited. 
Output
Your current score is 0. Keep playing!
Your current score is 1. Keep playing!
Your current score is 2. Keep playing!
Your current score is 3. Keep playing!
Your current score is 4. Keep playing!
An unexpected event occurred! Exiting the game early.
Game OverSummary
- Both codes are correct and will print the congratulatory message after the loop finishes.
 - Code 1: The message is outside the loop and always executes after the loop finishes.
 - Code 2: The message is in the 
elseblock and executes only if the loop terminates normally (i.e., withoutbreak). 
The choice between the two often depends on personal preference or specific requirements of your program. Using elsewith while can provide additional clarity in scenarios where distinguishing between normal and abnormal loop termination is important.