Reputation: 45
I have this problem with my code in prolog.
It is about fashion stylist expert system that takes properties from the user and gives her the specified look.
Here is the code:
main_level(2):-
write('Please enter weight (60s (1), 50s (2)): '), read(W),
write('Please enter tall (more than 160 (1), less than 160 (2)): '), read(T),
write('Please enter skin color (bronze (1), white (2), black (3)): '), read(S1),
write('Please enter hair length (medium (1), short (2), long (3)): '), read(H1),
write('Please enter hair color (brown (1), black (2), blond (3)): '), read(H2),
write('Please enter event (wedding (1), fami party (2): '), read(E),
But how I can force the user to enter the correct answer, so the program will not complete until the user enters the correct answer?
I tried to do it by using recursive rule but It didn't work either!
Upvotes: 3
Views: 544
Reputation: 9658
You can test if the answer is equal to the expected values, if not the rule fails, you can use a cut (!) after this check to show the error message as below.
main_level(2):-
write('Please enter weight (60s (1), 50s (2)): '),
read(W), (W == 1; W == 2),!,...
main_level(2):- write('Please enter a value according to the menu').
Upvotes: 1
Reputation: 60014
Basically, you could validate each input with this schema:
main_level(2):-
repeat, write('Please enter weight (60s (1), 50s (2)): '), read(W), (W == 1 ; W == 2),
repeat, write('Please enter tall (more than 160 (1), less than 160 (2)): '), read(T), (T == 1 ; T == 2),
...
but I would suggest instead to code a simple menu, like
menu(Header, Choices, Choice) :-
repeat,
write(Header),
forall(nth1(I,Choices,Value), format('~w (~d) ', [Value,I])),
read(C),
nth1(C, Choices, _). % validate index input
and call it with
main_level(2):-
menu('Please enter weight ',['60s','50s'], W),
menu('Please enter tall ',['more than 160','less than 160'], T),
...
Upvotes: 1