63.3k views
6 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?

User Rewritten
by
6.5k points

1 Answer

5 votes

Answer:

While loop

Step-by-step explanation:

It should be noted that the number of times which the computer will ask the user to take a guess is not known.

When we have a condition like this, the while loop (or in some programs; the do-while loop) is to be used.

To further back my point, see the following program in python.

secret_code = 1234

user_guess = int(input("Take a guess: "))

while user_guess != secret_code:

user_guess = int(input("guess again: "))

print("You guessed right")

The above program will keep asking the user to take a guess until the user enters 1234.

This may or may not be possible with a for loop.

User Jderda
by
6.6k points