User Input , How can we do it?

后端 未结 3 603
后悔当初
后悔当初 2020-12-31 10:51

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          


        
相关标签:
3条回答
  • 2020-12-31 11:13

    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 ..

    0 讨论(0)
  • 2020-12-31 11:17

    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).
    
    0 讨论(0)
  • 2020-12-31 11:21
    Animal('X').
    
    Input:- write("enter your name"),nl, 
            read(X), nl, 
            write(X).
    
    0 讨论(0)
提交回复
热议问题