Effacer les filtres
Effacer les filtres

I am having a hard time running my code. I do not know what I am doing wrong. How can I fix it.

1 vue (au cours des 30 derniers jours)
clc
yes = 0;
a=input('What is your weight? ');
b=input('Is this in lb or kg ?')
%score=input('Please input your score: ')
while yes == 0;
if d==lb;
X=a./(9./20);
disp(X)
disp('This is your weight in kg')
elseif d==kg;
Y=a.*(9./20);
disp(Y)
disp('This is your weight in lb')
end
yes=input('Do you like to do it again? if yes, press 0, if not press 1');
end
disp(' Thank you, Bye')

Réponses (4)

Image Analyst
Image Analyst le 4 Fév 2022
Lots of problems with that. I've corrected some of them. Try it this way:
clc
yes = 0;
weight = input('What is your weight? ');
units = input('Is this in lb or kg? ', 's')
%score=input('Please input your score: ')
while yes == 0
if contains(units, 'b', 'IgnoreCase', true)
X = weight ./ (9./20);
fprintf('This is your weight in kg : %.2f kg.\n', X)
else
Y = weight .* (9./20);
fprintf('This is your weight in pounds : %.2f lbs.\n', Y)
end
yes = input('Would you like to do it again? If yes, enter 0, if not enter 1 ');
end
disp(' Thank you. Bye')

Voss
Voss le 4 Fév 2022
Five things:
1) use the 's' option with input() if you want to take the user input exactly as it is (i.e., a character vector) without interpretation or conversion to numbers (see the input for lb or kg below)
2) you have to put single quotes around lb and kg in the comparison == because otherwise it refers to a variable called lb or a variable called kg, which don't exist
3) move the calls to input() inside the while loop in order to have the option to continue with another weight conversion
4) the conversions were switched around
5) there was no variable d; I changed it to b
Refer to the code below and compare to your original to see those differences. It could be cleaned up further, but now it runs as intended.
clc
yes = 0;
while yes == 0;
a=input('What is your weight? ');
b=input('Is this in lb or kg ?','s');
%score=input('Please input your score: ')
if b=='lb';
X=a.*(9./20);
disp(X)
disp('This is your weight in kg')
elseif b=='kg';
Y=a./(9./20);
disp(Y)
disp('This is your weight in lb')
end
yes=input('Do you like to do it again? if yes, press 0, if not press 1');
end
disp(' Thank you, Bye')

Walter Roberson
Walter Roberson le 4 Fév 2022
clc
I do not recommend clearing the command window inside a script, since the user might have something present on the screen that they want to read from later, such as to compare between different runs.
yes = 0;
a=input('What is your weight? ');
b=input('Is this in lb or kg ?')
%score=input('Please input your score: ')
while yes == 0;
The semi-colon there is not needed. The editor might give you a warning that your code would be better without it
if d==lb;
The semi-colon there is not needed. The editor might give you a warning that your code would be better without it.
You have not defined any variable named d in this code, and you have not defined any variable named lb in this code. In order for that line to work, you would have had to either already assigned to d and lb or else they would have to be functions.
It would be most natural to interpret your code as being intended to pay attention to the user's request for lb or kg, but you are not doing so -- the result of the user's request would be in b not in d
If you were to change that line to
if b==lb
then you would be paying attention to the user.
But then you have to ask what exact value the user entered. Do you expect them to enter the literal characters lb at the prompt? When you use input() with only one parameter, then MATLAB evaluates the user input as an expression, so if the user typed in lb then MATLAB would try to evaluate lb as an expression, and would look for a variable named lb or a function named lb .
If you use the 's' option of input() then MATLAB receives what the user types as just plain characters without trying to execute them. If you use that then b would contain 'lb' (a character vector of length 2) and you would be comparing it to the function or variable lb . What would be more likely is that you should use
if strcmp(b, 'lb')
which asks to compare character vectors. strcmp() can deal with the possibility of the character vectors being different lengths.
X=a./(9./20);
disp(X)
disp('This is your weight in kg')
elseif d==kg;
Y=a.*(9./20);
disp(Y)
disp('This is your weight in lb')
end
yes=input('Do you like to do it again? if yes, press 0, if not press 1');
end
disp(' Thank you, Bye')

Caleb Banga
Caleb Banga le 4 Fév 2022
Modifié(e) : Caleb Banga le 4 Fév 2022
THANK YOU ALL VERY MUCH! I see and understand my mistake.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by