I want to determine whether the user wishes to work with their angles in degrees or radians

3 Ansichten (letzte 30 Tage)
Hi, I have written the following code to determine whether the user want their angle to be calculated in degrees or in radians
type= input('Enter D to work angle in degrees or R for radians')
V= input('Enter velocity ');
a= input('Enter angle ');
cosine;
sine;
if (type==r | R)
cosine=cos;
sine=sin;
elseif (type==d | D)
cosine=cosd;
sine=sind;
else
output('You failed to input a correct answer');
end
It won't work for me. I would also liek to allow the user to be able to reinput their choice in the last else statement in case they hit an invalid key.
Thanks Chris

Antworten (2)

Walter Roberson
Walter Roberson am 30 Okt. 2012
input() expects numeric input unless you use the 's' option.
If you want to compare strings, use strcmp() or strcmpi()

dimitris
dimitris am 30 Okt. 2012
type= input('Enter D to work angle in degrees or R for radians')
V= input('Enter velocity ');
a= input('Enter angle ');
cosine;
sine;
if (type==r | R)
cosine=cos(a);
sine=sin(a);
elseif (type==d | D)
cosine=cosd(a);
sine=sind(a);
else
type=input('You failed to input a correct answer, press r for rads or d for degrees');
if (type==r | R)
cosine=cos(a);
sine=sin(a);
elseif (type==d | D)
cosine=cosd(a);
sine=sind(a);
else
end
Or you can use a while loop if you want the user to reinput his choice until it's correct.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by