User Input , How can we do it?

≡放荡痞女 提交于 2020-04-29 09:23:56

问题


How can we get something from user in prolog : for example :

animal(dog).
animal(cat).
write('please type animal name:'),nl.
/* How to read from user and store it to X 
and then check that user has typed animal name ?*/
?-animal(X).

回答1:


You can use read for that. For example you could write read(X), animal(X). into the prolog interpreter or write this into a script file:

:- read(X), animal(X).

If you then enter a valid animal name into the prompt, it will be bound to X. If you enter an invalid name, it won't.

Or you could define a procedure like this:

read_animal(X) :-
  write('please type animal name:'),
  nl,
  read(X),
  animal(X).

And then call it in the in the interpreter like read_animal(X)..

Note that the input needs to be terminated by a ..




回答2:


Animal('X').

Input:- write("enter your name"),nl, 
        read(X), nl, 
        write(X).



回答3:


Reading values

%                   name  id
 stud_name('ankit',01).
 stud_name('varun ',02).

Read_stud:-
  write("write name to know Id of student "),nl, 
  Read(Input),nl, 
  stud_name(Input,Output),nl,
  write(Output).


来源:https://stackoverflow.com/questions/5107745/user-input-how-can-we-do-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!