Preventing input errors from quitting script using while loop
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I'm writing a script that includes a prompt for the user to input either '1' or '0' and will record the response in an output txt file.
I want to limit the available responses to 1 and 0, and also prevent throwing an error and stopping the script if the user inputs a '-' or '`' by accident (which are next to the 0 and 1 keys).
Here's an example of I've got for now:
for k = 1:4
prompt2 = ('If CORRECT press 1, if INCORRECT press 0, then press return to advance: ');
%---Need to prevent recording non 1's and 0's
%---Need to prevent errors forcing quit (ONLY CATCHES ONCE?)
try
x_or_o = input(prompt2);
while ~isscalar(x_or_o) || (x_or_o ~= 1 && x_or_o ~= 0)
fprintf('\t***PLEASE PRESS 1 or 0 ONLY***\n');
prompt2 = ('If CORRECT press 1, if INCORRECT press 0, then press return to advance: ');
x_or_o = input(prompt2);
end
catch
warning('***PLEASE PRESS 1 or 0 ONLY***');
x_or_o = 2;
while ~isscalar(x_or_o) || (x_or_o ~= 1 && x_or_o ~= 0)
fprintf('\t***PLEASE PRESS 1 or 0 ONLY***\n');
prompt2 = ('If CORRECT press 1, if INCORRECT press 0, then press return to advance: ');
x_or_o = input(prompt2);
end
end
fprintf('Good job! Trial number %d finished!\n',k);
end
This mostly does what I want it to EXCEPT that it can't handle two consecutive errors, only one. Two consecutive inputs of '-' for example will make it quit.
I've tried a few different combinations of loops but I can't seem to get anything to handle errors how I want it to.
Any help would be greatly appreciated!
------------
Edit for clarity:
I'm trying to avoid a situation where a user enters a character that throws an error and exits the program. Instead, I want the user to be re-prompted to enter either 1 or 0 only no matter the number of incorrect inputs. Some specific examples using the above code might help me explain better.
Scenario 1: User only ever inputs 1's and 0's. Here's the command window output:
- >> input_error_handling
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 1
- Good job! Trial number 1 finished!
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 0
- Good job! Trial number 2 finished!
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 1
- Good job! Trial number 3 finished!
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 0
- Good job! Trial number 4 finished!
- >>
Scenario 2: User accidentally hits wrong key but not one that will throw an error:
- >> input_error_handling
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 0
- Good job! Trial number 1 finished!
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 1
- Good job! Trial number 2 finished!
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 2
- ***PLEASE PRESS 1 or 0 ONLY***
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 3
- ***PLEASE PRESS 1 or 0 ONLY***
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 9
- ***PLEASE PRESS 1 or 0 ONLY***
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 1
- Good job! Trial number 3 finished!
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 1
- Good job! Trial number 4 finished!
- >>
Scenario 3: User accidentally hits key that would throw error, but it's caught and their next input is correct:
- >> input_error_handling
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 1
- Good job! Trial number 1 finished!
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: `
- Warning: ***PLEASE PRESS 1 or 0 ONLY***
- > In input_error_handling (line 15)
- ***PLEASE PRESS 1 or 0 ONLY***
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 1
- Good job! Trial number 2 finished!
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: =
- Warning: ***PLEASE PRESS 1 or 0 ONLY***
- > In input_error_handling (line 15)
- ***PLEASE PRESS 1 or 0 ONLY***
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 9
- ***PLEASE PRESS 1 or 0 ONLY***
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 0
- Good job! Trial number 3 finished!
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 0
- Good job! Trial number 4 finished!
- >>
Scenario 4: The user accidentally inputs two error-throwing inputs in a row. This is the situation that the current code can't handle, and when the second error-throwing input is entered the script quits without finishing:
- >> input_error_handling
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: 1
- Good job! Trial number 1 finished!
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: `
- Warning: ***PLEASE PRESS 1 or 0 ONLY***
- > In input_error_handling (line 15)
- ***PLEASE PRESS 1 or 0 ONLY***
- If CORRECT press 1, if INCORRECT press 0, then press return to advance: `
- Error using input_error_handling (line 20)
- Error: Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.
- >>
Scenarios 1-3 can all be handled, it's just scenario 4 that's the problem. I'd like to find a solution to that, even if it's a relatively unlikely scenario.
0 Commenti
Risposte (1)
madhan ravi
il 3 Gen 2019
for k = 1:4
prompt2 = 'If CORRECT press 1, if INCORRECT press 0, then press return to advance: '
input(prompt2)
end
Gives error when input is '-' :
prompt2 =
'If CORRECT press 1, if INCORRECT press 0, then press return to advance: '
If CORRECT press 1, if INCORRECT press 0, then press return to advance: -
Error using COMMUNITY (line 3)
Error: Invalid expression. Check for missing or extra characters.
Note: input accepts only number if 's' is not mentioned (lookup doc).
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!