Hi, I am asking the user for input and the input should be either "AA" "Aa" or "aa". How can I validate the input using a while loop? I don't think my code works.Thanks!

1 view (last 30 days)
parentOneA = input("Enter Parent 1's A Trait: ")
% Error checking input
while parentOneA ~= "AA" || parentOneA ~= "Aa" || parentOneA ~= "aa";
parentOneA = input("Enter Parent 1's A Trait: ")
end
  2 Comments

Sign in to comment.

Answers (1)

Sindar
Sindar on 24 Sep 2020
You're requiring that it be all valid entries simultaneously
parentOneA = input("Enter Parent 1's A Trait: ")
% Error checking input
while parentOneA ~= "AA" && parentOneA ~= "Aa" && parentOneA ~= "aa";
parentOneA = input("Enter Parent 1's A Trait: ")
end
or
valid_inputs = {'AA';'Aa';'aa'};
parentOneA = input("Enter Parent 1's A Trait: ")
while ~any(strcmp(parentOneA,valid_inputs))
parentOneA = input("Enter Parent 1's A Trait: ")
end

Categories

Tags

Community Treasure Hunt

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

Start Hunting!