Help with restricting input
59 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
John Furman
le 29 Jan 2020
Commenté : Fangjun Jiang
le 29 Jan 2020
I am working on a script that asks for an input to be given between 1 and 100. I would like to have the restriction that the input is limited to between 1 and 100, and that the number must be a whole number.Here is the code:
number=input('Enter a whole number between 1 and 100:')
while number<1 || number>100
display('Not a valid number please try again')
number=input('Enter a whole number between 1 and 100:')
end
I can't figure out how to get it to set a restriction to non-whole number values. Please let me know how to specify this. I have been searching for a while and haven't found the solution.
0 commentaires
Réponse acceptée
Fangjun Jiang
le 29 Jan 2020
Suprisingly, just run your specification and it works in MATLAB
number=input('Enter a whole number between 1 and 100:')
while number<1 || number>100
display('Not a valid number please try again')
number=input('Enter a whole number between 1 and 100:')
end
2 commentaires
Fangjun Jiang
le 29 Jan 2020
change to
while number<1 || number>100 || round(number)~=number
Plus de réponses (1)
Adam
le 29 Jan 2020
validateattributes( number, { 'numeric' }, { 'scalar', '>=', 1, '<=', 100, 'integer' } )
You would have to put a message together yourself though if you don't want the error message itself returned on command line
0 commentaires
Voir également
Catégories
En savoir plus sur Whos dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!