How to get different input, depending on the last input

So I need to make a chatbot script and i'm stuck on this part
x = input('+ ', 's');
s={'hey','hi','hello'};
if ismember(x,s)
disp('Hello, What''s your name?')
else disp('Don''t you say hello?')
end
So at this point if the user inputs hello I can ask for the next input which is the name. But if they don't, it displays the else situation. How do I go a different direction if the user doesn't say 'hello'? So for example, after displaying the else, if the user inputs 'hello' the script would return 'That is better, hello back' or something.
Sorry if i'm unclear, thanks.

 採用された回答

Geoff Hayes
Geoff Hayes 2015 年 5 月 24 日

0 投票

ali - you could use a while loop to get the user to say/input 'hello' and then branch off to the next step which would be to get the name. Something like
x = input('+ ', 's');
s = {'hey','hi','hello'};
userIsRude = false;
while ~ismember(lower(x),s)
userIsRude = true;
disp('Don''t you say hello?');
x = input('+ ', 's');
end
if ~userIsRude
disp('Hello, What''s your name?');
else
disp('Finally...you said hello. What''s your name?');
end
You could then add the code to get the user to enter his/her name.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2015 年 5 月 24 日

回答済み:

2015 年 5 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by