Question

I want to ask about my multiple choice program that I made. I confuse how to store a value continuously, so I can take a conclusion in the end of program, based on sum of the value that already store.

start :- program.
program :- 
     write('This is question number 1'),nl,
     write('A'),nl,
     write('B'),nl,
     write('C'),nl,
     read(Choice),nl,
     ( 
        %case1
        Choice='a'->nl, 
        =================================
        A = A + 1  <--- Right Here,
        And then go to Question 2.
        =================================

        %case2
        Choice='b'->nl, 
        =================================
        B = B + 1  <--- Right Here,
        And then go to Question 2.
        =================================

        %case3
        Choice='c'->nl, 
        =================================
        C = C + 1  <--- Right Here,
        And then go to Question 2.
        =================================

        %case_default
        write('Wrong Code')
    ).

So I can take a conclusion like this,

===================================
if B < A > C then you're an A.
if A < B > C then you're a B.
if A < C > B then you're a C.
===================================

Thanks you very much before :)

Was it helpful?

Solution

There are many possible ways to store values. What you probably think of - storing some counters and updating them on given answers - can lead to some complicated situations. Example of code working with counters:

question('Choose a, b or c: ').
question('Choose again a, b or c: ').
question('Think before you choose a, b, or c: ').

ask :-
    findall(Q, question(Q), Questions),
    ask_and_count([0,0,0], [A,B,C], Questions),
    max_list([A,B,C], Max),
    nth1(Index,[A,B,C],Max),
    nth1(Index,[a,b,c],Answer),
    print('You''re a(n) '), print(Answer), nl.
ask_and_count(Count, Count, []) :-
    print('Thank you, that''s all questions!'), nl, !.
ask_and_count(InCount, OutCount, [Q | Questions]) :-
    print(Q),
    read_abc(Ans),
    increase(Ans, InCount, NewCount),
    ask_and_count(NewCount, OutCount, Questions).
read_abc(A) :-
    read(A),
    member(A, [a,b,c]),
    !.
read_abc(A) :-
    print('Only a, b and c are accepted! Answer again: '),
    read_abc(A).
increase(a, [A, B, C], [X, B, C]) :- X is A + 1.
increase(b, [A, B, C], [A, X, C]) :- X is B + 1.
increase(c, [A, B, C], [A, B, X]) :- X is C + 1.

Sample input and output:

?- ask.
Choose a, b or c: b.
Choose again a, b or c: c.
Think before you choose a, b, or c: c.
Thank you, that's all questions!
You're a(n) c

Maybe I can inspire you to do it another way, which in my opinion is more "prologish" as it relies on lists and leads to simpler code:

question('Choose a, b or c: ').
question('Choose again a, b or c: ').
question('Think before you choose a, b, or c: ').

read_abc(A, Choices) :-
    read(A),
    member(A, Choices),
    !.
read_abc(A, Choices) :-
    print('Only '), print(Choices), print(' are accepted! Try again: '),
    read_abc(A, Choices).
ask(Answers) :-
    findall(Ans, (question(Q), print(Q), read_abc(Ans, [a,b,c])), Answers).

start :-
    ask(Answers),
    print(Answers).

Sample input and output:

?- start.
Choose a, b or c: c.
Choose again a, b or c: b.
Think before you choose a, b, or c: c.
[c,b,c]
true.

After you have all answers in a list, you can simply count occurences of every answer.

Other way of storing choices is by using assert and retract predicates. Expert systems do that. When using assert and retract you can use some predicates like global variables known from other programming languages.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top