How can I keep asking the user for correct input even after they press enter?
Show older comments
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
More Answers (1)
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
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
on 29 Jan 2021
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!