155,595 views
32 votes
32 votes
Prompt the user to guess your favorite color. Using a while loop, if the user didn't guess your favorite color [pick one for this activity] then tell them they are incorrect, then ask them again. Whenever they guess the color correctly, output that they are correct and how many guesses it took them.

Create a variable and assign it the value of 1
Prompt the user to guess your favorite color
While their guess is not equal to your favorite color
Tell them they are incorrect
Prompt them to guess again
Add one to the variable above
Output to the user, they were correct and how many attempts it took using the variable

User Miturbe
by
3.3k points

2 Answers

20 votes
20 votes

Final answer:

In this programming exercise, students use a while loop to create a guessing game for the favorite color 'blue' and employ a counter to track the number of guesses until the user guesses correctly.

Step-by-step explanation:

This Python programming exercise can teach students about control structures, specifically the while loop. Let's say our favorite color for this activity is blue. We can start by creating a variable to count the number of guesses. Here's a basic structure of the code that fulfills the given requirements:

favorite_color = 'blue'
guess_count = 1
guess = input('Guess my favorite color: ')
while guess != favorite_color:
print('Sorry, that is incorrect. Try again.')
guess = input('Guess again: ')
guess_count += 1
print('Congratulations, you guessed it correctly! It took you', guess_count, 'guesses.')

This loop will continue to prompt the user for a guess until they input the string 'blue'. Once they guess correctly, it prints a congratulatory message and tells them how many attempts it took. This example helps to illustrate loop iteration and the use of a counter within the loop.

User Michael Haar
by
3.2k points
17 votes
17 votes

Final answer:

To create a guessing game with a while loop in programming, define a variable for the guess count, prompt for a guess, use a while loop to check the guess, and output the result once the correct guess is made.

Step-by-step explanation:

To create a program that prompts the user to guess your favorite color and uses a while loop to check their guess, you would do the following:

First, you'll need to create a variable to keep track of the number of guesses. You can assign it the value of 1 since the user hasn't made any guesses yet.

let guessCount = 1;

Next, prompt the user to guess your favorite color. In this scenario, let's say your favorite color is blue.

let userGuess = prompt("Guess my favorite color:");

Now set up a while loop that runs as long as the user's guess isn't correct. Inside the loop, inform them that they are incorrect, prompt them to guess again, and increment the guess count variable.

while (userGuess !== 'blue') {
alert("Incorrect, try again.");
userGuess = prompt("Guess my favorite color:");
guessCount++;
}

When the user finally guesses 'blue', the loop will exit. Provide feedback that they are correct and tell them how many guesses it took.

alert("Correct! It took you " + guessCount + " guesses.");

Using this logic, the user gets immediate feedback on their guesses and knows exactly how many attempts they made before they succeeded.

User Peladao
by
2.7k points