Clear Filters
Clear Filters

Nested While loop functions

8 views (last 30 days)
Spaceman on 8 Apr 2024
Commented: Spaceman on 16 Apr 2024
Given: I have created a simple number guessing game that prompts the user to guess a random number between 1 & 100 and will inform them if their guess is too high or too low, then tell them when they have guessed right.
Find: I need to find a way to use another while loop, (either inside of or side by side), to ask the user if they would like to play again after they have guessed the right answer.
Issue: I just don't even know where to begin. I got the first loop running smooth, but I am unsure of how to use another loop to loop that loop if prompted to do so by the user.
My Solution: I didn't suppress the output of num so I can troubleshoot the code.
guess=input('Welcome to the game! Guess a number between 1 & 100: ');
while num~=guess
if guess>100 || guess<1
disp('Invalid Entry')
elseif guess>num
disp('Your guess is too high')
disp('Your guess is too low')
guess=input('Enter a new number: ');
disp('You guessed right! What a surprise!')
fprintf('You guessed this many times: %i\n',count)
if count==1
disp('You cheated!')
elseif count<4
disp('Not bad.')
disp('Practice makes perfect...')
% My thinking is to somehow put my working "game" into another while loop
% and ask the user to input a 1 if they want to play again, or a 2 if not, but how would I do
% that? while r,(response)=1,(yes)? This would be my first nested loop.
Voss on 16 Apr 2024
That's what indentation is for. Each "end" lines up with its corresponding while/for/if.
Spaceman on 16 Apr 2024
It still hurts my brain, lol.

Sign in to comment.

Accepted Answer

Shubham on 8 Apr 2024
Hi Kyle,
To achieve the functionality of asking the user if they want to play the game again after they've guessed the number correctly, you can indeed use another while loop around your existing game loop. This outer loop will control whether the game restarts based on the user's input. Here's how you can structure it:
  1. Initialize a variable before the outer loop to control whether the game should continue. Let's call this variable playAgain and set it to true initially.
  2. Wrap your existing game code inside this outer while loop, which continues as long as playAgain is true.
  3. After the inner game loop (where the user has guessed the number correctly), prompt the user to ask if they want to play again.
  4. Based on the user's input, update the playAgain variable. If the user chooses to play again, the outer loop will continue; otherwise, it will exit, ending the game.
Here's how your code could look with these modifications:
playAgain = true; % Initialize the play again control variable
while playAgain
num = randi([1,100]); % Generate a random number
fprintf('Welcome to the game! Guess a number between 1 & 100: ');
guess = input('');
count = 1;
while num ~= guess
count = count + 1;
if guess > 100 || guess < 1
disp('Invalid Entry');
elseif guess > num
disp('Your guess is too high');
disp('Your guess is too low');
guess = input('Enter a new number: ');
disp('You guessed right! What a surprise!');
fprintf('You guessed this many times: %i\n', count);
if count == 1
disp('You cheated!');
elseif count < 4
disp('Not bad.');
disp('Practice makes perfect...');
% Ask the user if they want to play again
playAgainResponse = input('Do you want to play again? (yes=1/no=0): ');
if playAgainResponse == 1
playAgain = true; % User wants to play again
playAgain = false; % User does not want to play again, exit the loop
disp('Thanks for playing!');
In this code, after the user successfully guesses the right number, they are prompted to decide whether they want to play again. If the user inputs 1, the game restarts. If they input 0, the message 'Thanks for playing!' is displayed, and the program exits the outer loop, effectively ending the game. This approach allows for the game to be played multiple times without restarting the script manually.
I hope this helps!
  1 Comment
Spaceman on 14 Apr 2024
Eureka! This helped so much! I knew what I wanted to do but not quite how to go about it! Any idea how I would calculate the AVERAGE of all the guesses across all of the games played and then display then after the user has decided they no longer want to play? Thank you so much :)

Sign in to comment.

More Answers (0)


Find more on Just for fun in Help Center and File Exchange





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!