How can I keep asking the user for correct input even after they press enter?

22 views (last 30 days)
Hello,
I want my statement to continue to be prompted until correct input is given.
Prompt the user to write "ready" or "Ready" and keep prompting the user until they do so, and if they just pressed enter, a statement shows up asking them to "Please type "Ready"". I am a little lost as to how to do that using while(1) loop. I have this so far...
while (1)
Ready= input('When you are ready to play please type "Ready": ','s');
if Ready== 'ready' | Ready== 'Ready'
break
end
end
Please help
Thank you in advance.

Accepted Answer

Adam Danz
Adam Danz on 29 Jan 2021
Edited: Adam Danz on 29 Jan 2021
Most of the time case sensitivity shouldn't be a constraint in these types of queries. The demo below accepts any form of "ready" (READY, ready, Ready, reaDY, etc).
response = '';
while ~strcmpi(response,'ready')
response = input('When you are ready to play please type "Ready": ','s');
end
If you truly only want to accept Ready and ready, use this condition instead,
while ~any(strcmp(response, {'Ready','ready'}))
Another form that gives the user feedback on their error,
instructions = 'When you are ready to play please type "Ready": ';
baseInstructions = instructions;
response = '';
while ~strcmpi(response,'ready')
response = input(instructions,'s');
instructions = sprintf('%s, (you typed "%s") ',baseInstructions, response);
end
Another idea since this is a binary task (the user types the correct word or they do not), why not use a question or message box?
qdh = questdlg('When you are ready to play, please press go!', 'Game', 'Go!','Go!');
You could also add a "Cancel" option.
  5 Comments
Adam Danz
Adam Danz on 31 Jan 2021
I found a copy of the 5th ed of that book and strcmp or strcmpi where not mentioned in any of its 418 pages. I see the author is use R2013a which is really old (maybe the 6th ed has updates). strcmp/strcmpi are the go-to functions for string comparison. ismember is also often used along with lower() or upper() to allow for case insensitivity. But the equality test using "==" is definitely not recommended when comparing character arrays. For strings I guess it's OK but strings weren't even around in R2013a and still, the other functions I mentioned are better. isequal is another option but still not better than strcmp, strcmpi, or ismember.
JkBlack
JkBlack on 2 Feb 2021
Edited: JkBlack on 2 Feb 2021
Your help is very much appreciated! Thank you for pointing this out and sharing this information, will keep it in my mind.
Best Regards,
Jk

Sign in to comment.

More Answers (1)

per isakson
per isakson on 29 Jan 2021
Use
strcmpi( Ready, 'ready' )
instead of
Ready== 'ready' | Ready== 'Ready'
Don't use == in combination with character arrays to compare words. It compares character by character.
>> 'abc'=='a*c'
ans =
1×3 logical array
1 0 1
in contrary to strings
>> "abc"=="a*c"
ans =
logical
0
  2 Comments
per isakson
per isakson on 29 Jan 2021
"so I can't use strcmpi" What other function are excluded?
What about this one? Replace
if Ready== 'ready' | Ready== 'Ready'
by
if string(Ready)=="ready" || string(Ready)=="Ready"
JkBlack
JkBlack on 29 Jan 2021
Yes this worked! Thank you very much for sharing this information!
Best Regards,
JK

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!