Using Function to test height allowed on ride

조회 수: 3 (최근 30일)
Payton Philip Clark Chase
Payton Philip Clark Chase 2019년 12월 9일
댓글: Payton Philip Clark Chase 2019년 12월 9일
Many rides in amusement parks require riders to be a certain minimum height. Assume the minimum height is 48” for a ride. Write and test a function to determine whether the rider is tall enough.
MY CODE:
function [ans] = ride(height)
if (height<48)
height = 'no';
else
height = 'yes';
end
clear
clc
addpath('Library')
height = input('Whats your height: ');
ans = ride(height);
if strcmpi(ans, 'no')
disp('You cant ride')
elseif strcmpi (ans, 'yes')
disp('not yet')
end
I had very similar code to a classmate and wondering why it will not work? the error that occurs is "output argument "ans (and maybe others) not assigned during call to "ride"."
just wondering if anyone can help with this?

채택된 답변

Geoff Hayes
Geoff Hayes 2019년 12월 9일
Payton - where in your function, do you assign the result to ans
function [ans] = ride(height)
if (height<48)
height = 'no';
else
height = 'yes';
end
I think that you want to assign 'no' and 'yes' to ans instead of overwriting height. I would consider returning a boolean instead of yes/no strings.
  댓글 수: 1
Payton Philip Clark Chase
Payton Philip Clark Chase 2019년 12월 9일
Wow I can't believe I didn't see this hahaha
Thanks so much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by