Guessing game help (beginner programmer)
Show older comments
I need to create a guessing game program using while loops where the user needs to guess between 1 and 10 and they only get three tries. every time they get it wrong a message will display that their guess is wrong and to re enter another number. if its correct they will get a "right!" display but if they guess correct and tries exceeded 3 times a message will display that they got the right number but they exceeded tries and lost the game.
this is the program i have created so far and im stuck on the little things like how do i generate the program so that it allows 3 tries before you lose. this program might be a bit mixed up.
secretnumber = randi([1 10]);
numguesses = 0;
disp('I am thinking of a number from 0 to 10.');
disp('You must guess what it is in three tries.');
guess = input('Enter a guess: ');
while guess ~= secretnumber
numguesses = numguesses + 1;
if numguesses == secretnumber
disp('Right!');
fprintf('it took you %g guesses. You have won the game!');
end
endwhile
Accepted Answer
More Answers (0)
Categories
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!