Python like input() function?

534 views
Skip to first unread message

Ismael VC

unread,
Jan 18, 2014, 1:30:40 PM1/18/14
to julia...@googlegroups.com
Hello I needed a function like Python's input, here is what I did, but there is a single space when the function is called without arguments:


julia> function input(prompt::String = "")
           print(prompt * " ")
           chomp(readline())
       end

input
(generic function with 2 methods)
 
julia> input("Something here:")
Something here: hello
"hello"
 
julia> int(input("Enter a number:"))
Enter a number: 3
3
 
julia> typeof(ans)
Int64
 
julia> input() # I want ot get rid of the single space.
 
<--here is a sapce
"<--here is a sapce"


Is there somethign similar in Julia for this? How should I propose inclusion of such a function to Julia?

Thanks!

Ismael VC

unread,
Jan 18, 2014, 1:41:04 PM1/18/14
to julia...@googlegroups.com
Ok seems that python doesn't add a space after the prompt, so it would be something like this:

julia> function input(prompt::String = "")
           print(prompt)
           chomp(readline())
       end

input
(generic function with 2 methods)

The only difference besides the leading space, when called without arguments is that Python raises an EOFError if I type Ctrl+D, while my Julia function returns an empty string:

>>> input() # Python3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
EOFError




Ismael VC

unread,
Jan 18, 2014, 1:45:07 PM1/18/14
to julia...@googlegroups.com
Ok sorry! There is no leading spcae in my code anymore, I made the mistake to put a string with a space in the original code, the only difference with python is the EOFError, do you guys have a better idea?

Ismael VC

unread,
Jan 18, 2014, 1:55:10 PM1/18/14
to julia...@googlegroups.com
Well it acutally returns the empty string and then bails out of the julia REPL. What do you think should be the default behaviour for this?
Reply all
Reply to author
Forward
0 new messages