James Angelo
James Angelo

Reputation: 13

Yes No in Prolog if not (yes/no) then display invalid command(input)

I am creating an Expert System for Cell Phone Repair.

The answers must be just [yes or no], anything else will display "invalid input." How can i trap that in my code?

ask(Question) :-  write('Question: '),
      write(Question),
      write('? '),
      write('(yes or no) : '),
      read(Response),
      nl,
      ((Response == yes ; Response == y) -> assert(yes(Question)) ; 
       (Response==no ; Response ==n) -> assert(no(Question)) ;
        write('\nInvalid Input!!!\n'),fail).

I still can't implement what I want to display if any wrong spelling is entered.

Upvotes: 0

Views: 2811

Answers (1)

Aurélien Bénel
Aurélien Bénel

Reputation: 3842

Well... Your code definitely works. Just be sure to change assert with asserta or assertzif you use gprolog.

| ?- [expert].
compiling *** for byte code...
*** compiled, 7 lines read - 2111 bytes written, 14 ms
(1 ms) yes
| ?- ask(man).
Question: man? (yes or no): y.
yes
| ?- ask(woman).
Question: woman? (yes or no): no.
(1 ms) yes
| ?- ask(silly).
Question: silly? (yes or no): dunno.
Invalid Input!!!
no
| ?- yes(man).
yes
| ?- no(man).
no
| ?- yes(woman).
no
| ?- no(woman).
(1 ms) yes

Upvotes: 1

Related Questions