getting input from the user

The function read can be used to prompt the user and obtain a line of input as a string. In response to the prompt, the user enters sample and press return.

i1 : filename = read "file name : "
file name : sample

o1 = sample

o1 : String

Let's use peek to verify that this string contains no newline characters.

i2 : peek filename

o2 = "sample"

o2 : String

If necessary, we may use value to evaluate the string provided by the user. In this example, we enter (x+y)^2 in response to the prompt.

i3 : R = ZZ[x,y];
i4 : s = read "polynomial : "
polynomial : (x+y)^2

o4 = (x+y)^2

o4 : String
i5 : value s

      2           2
o5 = x  + 2x*y + y 

o5 : R


topindexpreviousupnext