I am having a problem in solving a coursera assignment can you please help me

2 次查看(过去 30 天)
The problem is as such: variable input function
Function called should take two arguments age and year,The function must return true if age is less than the limit. If limit is not mentioned it must have default as 21.Output argument name is too_young.
I have wrote a code can you please verify it,
function too_young=under_age(age,limit)
if nargin<2
limit=18;
end
if limit<18
too_young=true;
else
too_young=false;
end
Function call is as such:
too_young = under_age(18,18)
too_young = under_age(20)
I got the errors as such:
correct for 18 vs 18
Assessment result: incorrect20
Variable too_young has an incorrect value.
Assessment result: correct22
Assessment result: incorrect17 vs 18
Variable too_young has an incorrect value.
Assessment result: incorrect13 vs 12
Variable too_young has an incorrect value.
Assessment result: incorrectRandom tests
Variable too_young has an incorrect value.
under_age(21, 23) failed ...

采纳的回答

Geoff Hayes
Geoff Hayes 2020-4-24
Nikhil - Iook closely at this code
if limit<18
too_young=true;
else
too_young=false;
end
Why isn't age being used? What happens if the user has passed in a valid limit and now you are ignoring it by using 18? Try using the MATLAB debugger to step through the code and understand why the condition for your if statement is incorrect.

更多回答(1 个)

HARSHA VARDHAN
HARSHA VARDHAN 2020-12-10
function too_young=under_age(age,limit)
if nargin<2
limit=21;
end
if age<limit
too_young=true;
elseif age>=limit
too_young=false;
end

类别

Help CenterFile Exchange 中查找有关 Platform and License 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by