257,796 views
11 votes
11 votes
What kind of loop should be used in a game where the user is guessing a secret code and the message ""guess again!"" appears until the user guesses correctly? group of answer choices

a. for loop
b. binary loop
c. digital loop
d. while loop

User Gavin Anderegg
by
3.3k points

1 Answer

10 votes
10 votes

Answer:

In this case, the best type of loop to use would be a d. while loop.

Step-by-step explanation:

while loop will execute a block of code repeatedly as long as a certain condition is met. In this case, the condition would be whether the user has correctly guessed the secret code.

The loop would continue to run and display the message "guess again!" until the user enters the correct secret code. Here is an example of how the loop could be implemented in Python:

secret_code = "xyz"

guess = input("Enter your guess for the secret code: ")

while guess != secret_code:

print("guess again!")

guess = input("Enter your guess for the secret code: ")

print("Congratulations, you have guessed the secret code correctly!")

In this example, the while loop will continue to run as long as the value of guess is not equal to the value of secret_code. Each time through the loop, the user is prompted to enter a new guess, and the loop checks to see if the guess is correct. Once the user enters the correct secret code, the loop will exit and the message "Congratulations, you have guessed the secret code correctly!" will be displayed.

So to answer the question, the correct answer is d. while loop.

User Pabloelustondo
by
3.1k points