Random numbers between 1 and 10 with limited attempts?

10 visualizaciones (últimos 30 días)
Keeks
Keeks el 16 de Feb. de 2016
Comentada: Matthew Eicholtz el 17 de Feb. de 2016
Hello
I haven't found anything by searching this site, on the other hand I am new to this. I really would appreciate it if you could help me out on this one: I try to make a program which selects a random number between 1 and 10. One gets a limited amount of attempts to guess the right number. After 5 attempts, the programm should stop and tell you that you didn't succeed. I wrote some code and it does everything except stopping when one guessed the right number before attempt 5.
I tried changing everything but I only ended up in an infinite loop, after two hours I really would like to get help. :D
This is what I wrote:
Pickme=round(10*rand(1)); %Pick random number between 1 and 10
Guessedcorrectly=0;
while Guessedcorrectly == 0 %As long as the correct number has not been picked
for i=1:5
A(i)=input('Enter a number between 1 and 10:');% ask for new input
if A(i) == Pickme %Check whether number is correct
Guessedcorrectly = 1; %if so, have while-loop stop
disp('Correct! You have picked the right number!') %print message on screen
else disp('Wrong! Try again.') %if not, print on screen
end
end
end
The preview tells me that after for i=1:5, all this is put in a code/quote. I don't know why it does that. Can someone help me with my problem, i.e. why doesn't the script stop after one guessed the right number?
Greetings,
Keeks

Respuesta aceptada

Matthew Eicholtz
Matthew Eicholtz el 16 de Feb. de 2016
Editada: Matthew Eicholtz el 16 de Feb. de 2016
The script doesn't stop because it wants to finish the for-loop first. It will not check the line 'while Guessedcorrectly == 0' until the for-loop is done. There are many ways to solve this issue, but perhaps the easiest is to amend your code as follows:
...
if A(i) == Pickme %Check whether number is correct
Guessedcorrectly = 1; %if so, have while-loop stop
disp('Correct! You have picked the right number!') %print message on screen
break; % this tells MATLAB to leave the for-loop
...
  2 comentarios
Keeks
Keeks el 16 de Feb. de 2016
Editada: Keeks el 16 de Feb. de 2016
Oh my god this actually works, thank you so much! I tried "stop" but Matlab wouldn't listen to me.. now I know why! I just saw you posted an alternate script, thanks for the extra effort! It seems to be more advance than mine... How do you get to know all these commands? I always try to look up things in the help-function of MATLAB but it is until now that I came across statements like "isequal" instead of "==".
One question: Is it adviseable to avoid for-loops? I mean now I know I can always use "break", but when I have to write bigger scripts it could become a waste of memory I guess.
P.S.: Where is "%d" defined? It obviously has to be a MATLAB function because you didn't write what the script should do with it, but how does "%d" know to count down from 10? Due to maxcount? How then? Aah nevermind I get it, maxcnt-cnt.
PPS: I added a "break" to make your new script stop if one gave the correct answer before the counter hits 10, otherwise it would continue counting I guess. :)
Matthew Eicholtz
Matthew Eicholtz el 17 de Feb. de 2016
Glad I could help, Keeks. I've been using MATLAB for 10+ years now, but I still learn new commands and tricks quite often. If you want to improve your MATLAB knowledge, keep using help and doc, search the documentation/forums online, test your skills with Cody, and keep practicing! You'll be fluent in MATLAB before you know it.
1. For-loops are not inherently bad, but I try to avoid them when possible (especially if computations can be vectorized). Nested for-loops can be a time sink.
2. The '%d' is a formatting operator that tells the fprintf function to look for a signed integer (in the first case, cnt) and insert that in the string. Check out the documentation for more info.
doc fprintf
3. Good thinking on using break in the while-loop of my alternate code, but it is not necessary in this case because my loop checks if the user guessed correctly before asking them to provide another guess.

Iniciar sesión para comentar.

Más respuestas (1)

Matthew Eicholtz
Matthew Eicholtz el 16 de Feb. de 2016
Editada: Matthew Eicholtz el 16 de Feb. de 2016
Here is one alternative approach that removes the for-loop altogether:
pickme = randi(10); %the number we want the user to guess
cnt = 1;
maxcnt = 5; %max number of guesses
iscorrect = false;
while ~iscorrect && cnt<=maxcnt
x = input('Pick a number between 1 and 10: ');
if isequal(x,pickme)
fprintf('Correct! And it only took you %d tries!\n',cnt);
else
fprintf('Wrong! Try again. You have %d guesses left.\n',maxcnt-cnt);
cnt = cnt+1;
end
end

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by