Reputation: 13
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
Reputation: 3842
Well... Your code definitely works. Just be sure to change assert
with asserta
or assertz
if 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