A code will prompt the user to try to guess my favourite colour and after 3 goes say 3 trials over, but i want to display a message after each incorrect attempt saying "Incorrect try again". How do I do this? (New user)

21 vues (au cours des 30 derniers jours)
This is code i have got so far:
fav = 'red' ;
str = 0 ;
count = 0 ;
while str == 0
count = count+1 ;
prompt = 'What is my favourite color? ';
str2 = input(prompt,'s') ;
if strcmpi(fav,str2)
str = 1 ;
disp('you got the color correct')
elseif count == 3
str = 1 ;
disp('three trials over')
end
end

Réponse acceptée

Yusuf Azzam
Yusuf Azzam le 25 Fév 2019
I've got it! After 'disp('three trials over')' I add:
else
disp('incorrect')

Plus de réponses (1)

Brian Hart
Brian Hart le 23 Fév 2019
add the code
disp('Incorrect try again')
right before your very last end statement
  2 commentaires
Image Analyst
Image Analyst le 23 Fév 2019
And if they guessed correctly, it will still say that.
Yusuf Azzam
Yusuf Azzam le 25 Fév 2019
Yes, when I added it it still says incorrect try again when they guess correctly AND after they run out of chances, this is not what i want. Is there another way?

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by