Why does function create 'ans' output when it is already assigned?
Mostrar comentarios más antiguos
a code i've done for my assignment, except upon choosing '0' to exit the while loop, the function creates an 'ans' variable which is unwarranted.
If the entire code is ran inside a script, this does not occur. How may i suppress this? Unless i'm missing something obvious i am puzzled how this is happening. Thank you

Edit: Actual code removed once problem was fixed to avoid future plagiarising of assignment
Respuesta aceptada
Más respuestas (1)
How about now?
function [winner] = myfunction()
% Initialisation of scores
win = 0;
lose = 0;
draw = 0;
fprintf('What move do you play? \n ');
fprintf('1 - rock, 2 - paper, 3 - scissors \n 0 - quit game \n');
player_move = input('Please make your choice: ');
while player_move ~=0
% error check
while player_move < 1 || player_move > 3;
player_move = input('Invalid! Please make your choice form 1, 2, or 3: ');
end
if player_move == 1
fprintf('you play rock \n');
elseif player_move == 2
fprintf('you play paper \n');
else
fprintf('you play scissors \n');
end
comp_move = randi([1 3]);
if comp_move == 1
fprintf('Computer plays rock \n');
elseif comp_move == 2
fprintf('Computer plays paper \n');
else
fprintf('Computer plays scissors \n');
end
% Creation of a table with outcomes
% [rock(1), paper(2), scissors(3) by rock(1), paper(2),scissors(3)]
% column = player moves, row = computer generated move
% outcome of 1 if player wins, -1 if computer wins, 0 for draw
Rock = [0,-1,1];
Paper = [1,0,-1];
Scissors = [-1,1,0];
table = [Rock; Paper; Scissors];
[winner] = table(player_move, comp_move);
if winner == 1;
win = win + 1;
fprintf('You win! ')
elseif winner == -1;
lose = lose + 1;
fprintf('You lose! ')
else winner = 0;
draw = draw + 1;
fprintf('That''s a draw! ')
end
fprintf('Total: %d win, %d draw, %d lose \n \n', win, draw, lose);
fprintf('What move do you play? \n ');
fprintf('1 - rock, 2 - paper, 3 - scissors \n 0 - quit game \n');
player_move = input('Please make your choice: ');
if player_move == 0
fprintf('Good Bye! \n')
end
end
end
2 comentarios
Ali Tevs
el 18 de Oct. de 2016
Guillaume
el 18 de Oct. de 2016
Siva corrected the cause of ans output of the function (but still left the other bug in your code).
The other cause of ans output is simply due to the way you call the function and nothing to do with the function code itself.
You don't get an output if you input 0 initially because in that case winner has never been assigned, so there's nothing to send to ans. If you input something other than 0, then winner gets assigned a value to when the function returns you get an output. To fix this, call the function with a semicolon (as per my answer).
Note that the above also highlight another problem with your function. If you call it with:
>>result = rps; %with or without semicolon
and input 0 straight away, then you'll get an error because the requested output has never been assigned
Output argument "winner" (and maybe others) not assigned during call to "rps".
Categorías
Más información sobre Code Performance en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!