Error undefined variable or method in script

i don't understand why am i getting this error
i am using matlab online.
clear
clc
my_num = randi(30);
x = input('Enter a number : \n');
guess_the_number(x);
function guess_the_number(n)
if n == my_num
fprintf('Well you guessed it , %d.\n', a)
elseif n > 30
fprintf('Too Large bud, wanna go smaller maybe')
else
fprintf('Not correct, but nice try, number was %d', a)
end
end
Error is this
Enter a number :
12
Unrecognized function or variable 'my_num'.
Error in guess_the_number>guess (line 10)
if n == my_num
Error in guess_the_number (line 6)
guess(x);

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 5 月 20 日
編集済み: Ameer Hamza 2020 年 5 月 20 日

0 投票

You need to pass the value of 'a' and 'myNum' to the function
clear
clc
my_num = randi(30);
x = input('Enter a number : \n');
a = 2;
guess_the_number(x, my_num, a);
function guess_the_number(n, my_num, a)
if n == my_num
fprintf('Well you guessed it , %d.\n', a)
elseif n > 30
fprintf('Too Large bud, wanna go smaller maybe')
else
fprintf('Not correct, but nice try, number was %d', a)
end
end
An alternative is to use nested functions.

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

質問済み:

2020 年 5 月 20 日

編集済み:

2020 年 5 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by