Nested While loop functions
Show older comments
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.
num=randi([1,100])
guess=input('Welcome to the game! Guess a number between 1 & 100: ');
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')
else
disp('Your guess is too low')
end
guess=input('Enter a new number: ');
end
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.')
else
disp('Practice makes perfect...')
end
% 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.
12 Comments
Voss
on 8 Apr 2024
I think your idea to wrap this code in another while loop wherein the user is asked whether they want to play again is a good approach.
Try it out and see what happens. If you get stuck, post your updated code.
Hint: @Kyle Weaver Use of defining custom functions is better in this context and call the function inside another while loop
Spaceman
on 8 Apr 2024
Spaceman
on 8 Apr 2024
@Kyle WeaverThe answer to this question is present in your question. Make the loops nested (one within another)
while 1 % repeat the whole thing till the user wants to quit
x=input('Do you want to play again? (y)1 (n)2: ');
if x==1
guess=input('Welcome to the game! Guess a number between 1 & 100: ');
num=randi([1,100]);
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')
else
disp('Your guess is too low')
end
guess=input('Enter a new number: ');
end
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.')
else
disp('Practice makes perfect...')
end
else
disp('Have a nice day!')
break % give a break if the user wants to quit
end
end
Spaceman
on 14 Apr 2024
Voss
on 14 Apr 2024
That's right: break stops execution of the for or while loop it's located in; execution resumes after the loop's end statement.
If you wanted to break out of multiple nested loops at once, you'd need to create a variable that tells the program whether to break out of the outer loops or not. Example:
do_break_all = false;
while true
while true
while true
x = rand();
if x < 0.01
do_break_all = true;
break
end
end
if do_break_all
break
end
end
if do_break_all
break
end
end
Spaceman
on 15 Apr 2024
Voss
on 15 Apr 2024
All right. Forget about that example then.
Spaceman
on 15 Apr 2024
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
Accepted Answer
More Answers (0)
Categories
Find more on Board games 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!