M
Modularβ€’15mo ago
aviral shastri

Mojo user input

how can i take user input in mojo
37 Replies
TeamPuzel
TeamPuzelβ€’15mo ago
You could use scanf or python interop for this, I will try show an example
4a6f6e69
4a6f6e69β€’15mo ago
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
from python import Python

fn ask_name() raises -> String:
let user_input = Python.evaluate("input('What is your name? ')").to_string()
return user_input
aviral shastri
aviral shastriOPβ€’15mo ago
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
TeamPuzel
TeamPuzelβ€’15mo ago
you must also have fn main() raises:
aviral shastri
aviral shastriOPβ€’15mo ago
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
TeamPuzel
TeamPuzelβ€’15mo ago
Well. fn main() raises:, not fn main() raises -> String:
aviral shastri
aviral shastriOPβ€’15mo ago
lets goo its working thanks
TeamPuzel
TeamPuzelβ€’15mo ago
Yay good luck with whatever you're trying to do
aviral shastri
aviral shastriOPβ€’15mo ago
another doubt
ModularBot
ModularBotβ€’15mo ago
Congrats @aviral shastri, you just advanced to level 1!
aviral shastri
aviral shastriOPβ€’15mo ago
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)
TeamPuzel
TeamPuzelβ€’15mo ago
You would do it like in python yes
aviral shastri
aviral shastriOPβ€’15mo ago
not working
TeamPuzel
TeamPuzelβ€’15mo ago
what's the error?
aviral shastri
aviral shastriOPβ€’15mo ago
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
TeamPuzel
TeamPuzelβ€’15mo ago
Oh I see
aviral shastri
aviral shastriOPβ€’15mo ago
hey i got something its giving me suggestion to report bug on github
TeamPuzel
TeamPuzelβ€’15mo ago
from python import Python

fn main() raises:
let user_input1 = Python.evaluate("input('Num 1')")
let user_input2 = Python.evaluate("input('Num 2')")
let add = user_input1 + user_input2
print(add)
from python import Python

fn main() raises:
let user_input1 = Python.evaluate("input('Num 1')")
let user_input2 = Python.evaluate("input('Num 2')")
let add = user_input1 + user_input2
print(add)
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
aviral shastri
aviral shastriOPβ€’15mo ago
but this is not adding numbers its just adding it as a string yep
TeamPuzel
TeamPuzelβ€’15mo ago
from python import Python

fn main() raises:
let user_input1 = Python.evaluate("int(input('Num 1'))").to_float64()
let user_input2 = Python.evaluate("int(input('Num 2'))").to_float64()
let add: Float64 = user_input1 + user_input2
print(add)
from python import Python

fn main() raises:
let user_input1 = Python.evaluate("int(input('Num 1'))").to_float64()
let user_input2 = Python.evaluate("int(input('Num 2'))").to_float64()
let add: Float64 = user_input1 + user_input2
print(add)
There :D You have to first convert the python string to a python number
aviral shastri
aviral shastriOPβ€’15mo ago
its working ye can i use external pip installed modules in mojo
TeamPuzel
TeamPuzelβ€’15mo ago
I'm not sure, you can try I think there's examples of how to import python modules
aviral shastri
aviral shastriOPβ€’15mo ago
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
phuc
phucβ€’10mo ago
Hi @aviral shastri @TeamPuzel Is there any better way to take user input in latest version of Mojo (v0.6.3)?
Melody Daniel
Melody Danielβ€’10mo ago
The latest version did not include any new features
Jimmy Smith Jr.
Jimmy Smith Jr.β€’10mo ago
When I needed to read user input, I went like
with open("/dev/stdin", "r") as stdin:
let input = stdin.read()
with open("/dev/stdin", "r") as stdin:
let input = stdin.read()
Though this will only work on linux and mac, I guess.
phuc
phucβ€’10mo ago
Hi @Jimmy Smith Jr. does it only work on Windows?
ModularBot
ModularBotβ€’10mo ago
Congrats @phuc, you just advanced to level 1!
Jimmy Smith Jr.
Jimmy Smith Jr.β€’10mo ago
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.
phuc
phucβ€’10mo ago
Thanks for your clarification @Jimmy Smith Jr.
phuc
phucβ€’10mo ago
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...
mizoru
mizoruβ€’10mo ago
are you able to indicate EOF with this method in any way?
ModularBot
ModularBotβ€’9mo ago
Congrats @Jimmy Smith Jr., you just advanced to level 4!
Jimmy Smith Jr.
Jimmy Smith Jr.β€’9mo ago
By default this reads until it hits EOF if that is what you mean.
mizoru
mizoruβ€’9mo ago
yes, what I'm saying is that I was not able to input the EOF in any way
Jimmy Smith Jr.
Jimmy Smith Jr.β€’9mo ago
In a file, it's there by default, in the command line it should be Ctrl + D
Want results from more Discord servers?
Add your server