37 Replies
You could use scanf or python interop for this, I will try show an example
Something I tried and worked looked like this:
from python import Python
fn ask_name() raises -> String:
let user_input = Python.evaluate("input('What is your name? ')").to_string()
return user_input
fn main():
let a=ask_name()
print(a)
test.mojo:8:19: error: cannot call function that may raise in a context that cannot raise
let a=ask_name()
~~~~^~
test.mojo:8:19: note: try surrounding the call in a 'try' block
let a=ask_name()
^
test.mojo:7:1: note: or mark surrounding function as 'raises'
fn main():
^
mojo: error: failed to parse the provided Mojo
its giving errors
you must also have
fn main() raises:
from python import Python
fn main() raises -> String:
let user_input = Python.evaluate("input('What is your name? ')").to_string()
print(user_input)
test.mojo:3:1: error: expected 'main' function to return 'None'
fn main() raises -> String:
^
mojo: error: failed to parse the provided Mojo
still errors
Well.
fn main() raises:
, not fn main() raises -> String:
lets goo
its working
thanks
Yay
good luck with whatever you're trying to do
another doubt
Congrats @aviral shastri, you just advanced to level 1!
how can i get integer input
or i have to directly use python eval
from python import Python
fn main() raises:
let user_input1= Python.evaluate("input('Num 1')").to_int()
let user_input2= Python.evaluate("input('Num 2')").to_int()
let add: Int= user_input1 + user_input2
print(add)
You would do it like in python yes
not working
what's the error?
test.mojo:6:31: error: cannot implicitly convert 'PythonObject' value to 'Int' in 'let' initializer
let add: Int= user_input1 + user_input2
~~^~~
mojo: error: failed to parse the provided Mojo
Oh I see
I don't think you have to_int() https://docs.modular.com/mojo/stdlib/python/object.html#to_float64
Modular Docs - object
Module
hey
i got something
its giving me suggestion to report bug on github
it works if you don't use Int or Float
but this just uses Python numbers
I'm not sure how you would convert them to Mojo numbers like Int
no actually it's adding strings lol
but this is not adding numbers its just adding it as a string
yep
There :D
You have to first convert the python string to a python number
its working
ye
can i use external pip installed modules in mojo
I'm not sure, you can try
I think there's examples of how to import python modules
yes but it will be third party for mojo like first install it in pythno then python will check then mojo will check and use
Hi @aviral shastri @TeamPuzel Is there any better way to take user input in latest version of Mojo (v0.6.3)?
The latest version did not include any new features
When I needed to read user input, I went like
Though this will only work on linux and mac, I guess.
Hi @Jimmy Smith Jr. does it only work on Windows?
Congrats @phuc, you just advanced to level 1!
I was wrong with the wording, it will only not work on Windows π
I edited my comment.
Currently mojo is not supported on Windows, but if it will be, this approach will not work.
Thanks for your clarification @Jimmy Smith Jr.
I wrote my approaches here: https://coflutter.com/mojo-how-to-input-an-integer/
I am not sure if they are good or not. Just a note for myself and probably someone who needs
Phuc Tran
Coflutter
Mojo - How to input an integer - Coflutter
Version: mojo 0.6.3 (f58f8b94) If you come from other programming languages, the βinputβ function is supported natively to take the input from the user. The names are various depending on the languages (prompt() in Javascript, input() in Python, stdin.readLineSync() in Dart). Below is an example in Python using input() function: At the time I am...
are you able to indicate EOF with this method in any way?
Congrats @Jimmy Smith Jr., you just advanced to level 4!
By default this reads until it hits EOF if that is what you mean.
yes, what I'm saying is that I was not able to input the EOF in any way
In a file, it's there by default, in the command line it should be
Ctrl + D