Create a guessing game that will run until the user guesses the correct number.
ID: 3205758 • Letter: C
Question
Create a guessing game that will run until the user guesses the correct number. (1) Present brief instructions for the user, (2) generate one random number between 1 and 10, inclusive, (3) allow the user to guess the random number, (3) after each guess, tell the user if their guess was too high, too low, or correct. Upon completion of the program, display how many attempts it took to get the right answer. See the example output below for reference: Enter a guess: 2 Incorrect, your guess was too low. Enter a guess: 8 Incorrect, your guess was too high. Enter a guess: 4 Congratulations! You guessed the right number! It took you 3 tries to guess correctly.Explanation / Answer
N=10; % range desired
y=randperm(N);
k=y(1)
prompt = 'Enter a guess';
x = input(prompt)
chance=1;
while k~=x
if x < k
fprintf('Incorrect, your guess was too low ')
else
fprintf('Incorrect, your guess was too high ')
end
prompt = 'Enter a guess';
x = input(prompt);
chance= chance+1;
end
fprintf('It took %i chances for you to be correct ',chance)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.