问题
So below is the module of code I have created to verify a user input is between a range but this error keeps appearing below it ' statement expected but function found', any ideas anyone? Many thanks
function get_choice(var Options: integer): integer;
var
choice: integer;
begin
while (true) do
begin
write('choose option 1 to', Options);
try
readln(choice);
if (choice>=1) and (choice <=Options) then
get_choice := choice
else
write('invalid range');
except
write('not a number');
end;
end;
回答1:
You missed an end
:
try
readln(choice);
if (choice>=1) and (choice <=Options) then
get_choice := choice
else
write('invalid range');
except
write('not a number');
end;
end
end;
Try try..except
block must have its own end.
回答2:
As Sharam already stated, you missed the end
of the try..except..end
block. Nevertheless, I think I can enhance the answer a bit with some tips, to help avoiding this in you future.
- keep your (
begin .. end
blocks on the same column. Same fortry..except..end
,try..finally..end
andrepeat..until
blocks off course. - make use of the IDE's option to connect the begin and and blocks with lines. "structural highlighting. Check the attached screenshots.
来源:https://stackoverflow.com/questions/50855823/statement-expected-but-function-found-error-in-my-function-code