[Solved] Check input numbers in prolog


Since you tried something for your isDigit/1 predicate I’ll help about that :

Version with an if structure :

isDigit(X) :-
    (   number(X),
        X >= 0,
        X =< 8
     -> true
     ;  writeln('[0-8] AND Number'),
        fail).

Version with a disjunction (as you tried in your OP) :

isDigit(X) :-
    number(X),
    X >= 0,
    X =< 8,
    !
    ;
    writeln('[0-8] AND Number'),
    fail.

The problem you had in your attempt was that you let a choice point when X was a number and meeting the right requirements, so the predicate would go there and print the error message and fail anyway. You used cut in the wrong part of the disjunction (correct version just above !).

Now, for the thing where you want to check if a number has already been given, please give it a try, edit your OP, and if you struggle somewhere we’ll help.

3

solved Check input numbers in prolog