How to take user input form the terminal?

Self-explanatory, the best I could find with research was:
from python import Python

fn main() raises:
var a = Python.evaluate("input('What is your name? ')").to_string()
print(a)
from python import Python

fn main() raises:
var a = Python.evaluate("input('What is your name? ')").to_string()
print(a)
I got this error: What is your name? pedro Unhandled exception caught during execution: 'str' object has no attribute 'to_string' mojo: error: execution exited with a non-zero result: 1 So my questions: 1. How can I do it? Thank you.
20 Replies
bunny
bunny8mo ago
@MyriadColors , the error it telling you that you're trying to do to_string on a str object. So, it's already a string. If you just remove .to_string() then it works. See attached pics. Also, check out @toasty 's repo: https://github.com/thatstoasty/prism It's more for making a CLI tool (a'la Go's famous "Cobra" library) and not so much for holding a terminal-conversation, but I'm sure you could achieve that, too.
GitHub
GitHub - thatstoasty/prism: Mojo CLI Library modeled after Cobra.
Mojo CLI Library modeled after Cobra. Contribute to thatstoasty/prism development by creating an account on GitHub.
No description
No description
bunny
bunny8mo ago
erk..... didn't mean to ping you, toasty. I meant to make that a silent "no ping" at-tag before posting. Sorry. 😐
MyriadColors
MyriadColorsOP8mo ago
Thank you!
ModularBot
ModularBot8mo ago
Congrats @MyriadColors, you just advanced to level 1!
MyriadColors
MyriadColorsOP8mo ago
I just copied and pasted the code as I assumed it was needed. Though I really do find it weird that mojo doesnt have this feature as part of the language
bunny
bunny8mo ago
You good. The beautiful thing about stumbling around with Mojo: we're all stumbling around with Mojo. It's new; we're all noobs. 😂
MyriadColors
MyriadColorsOP8mo ago
Im not compl,aining btw, its just something I figured every language would have
bunny
bunny8mo ago
oh, give it time. The Mojo team has a stated goal of being a superset of Python. That means implementing stuff like input(), too. But it's brand-spanking-new. Just give 'em time. 🙂
MyriadColors
MyriadColorsOP8mo ago
Cool, hopefully we get something like std::cout instead of printf to make it easier 😛 also, sad to see f-strings doesnt work
bunny
bunny8mo ago
also, you could always contrib to the std-lib! If they just add some screaming guitar solos in the background, then this new vid would be a good motivational vid. 😄 https://www.youtube.com/watch?v=TJpFSSIts5Q
Modular
YouTube
Contributing to Open-Source Mojo🔥 Standard Library
Mojo🔥 standard library is now open-source. In this video Modular engineer Joe Loser discusses how you can get started with contributing to Mojo🔥 using Mojo🔥 nightly builds. Chapters: 00:00 Contributing to Mojo🔥 Standard Library 01:59 Demo: Contributing a simple test case Resources: Blog post: https://www.modular.com/blog/how-to-contribute-to-...
bunny
bunny8mo ago
Given that they're aiming for pythonic syntax, I'd expect it to remain print(). And I agree 100% re f-strings. Anytime I'm in any language that doesn't have them, I find that I really miss my f-strings.
MyriadColors
MyriadColorsOP8mo ago
Trying to mark your answer as the solution.
toasty
toasty8mo ago
@MyriadColors Try this/
from python import Python

fn main() raises:
var a = str(Python.evaluate("input('What is your name? ')"))
print(a)
from python import Python

fn main() raises:
var a = str(Python.evaluate("input('What is your name? ')"))
print(a)
It is possible to do it all in Mojo, but it's not simple as it would involve setting your terminal to raw mode via an external_call to C and capturing N bytes from stdin. If you're interested I could link you to an example from my termios repo
MyriadColors
MyriadColorsOP8mo ago
Would it work on linux? I know a bit of C
toasty
toasty8mo ago
It should work on linux, but you might need to mess with the structs a bit since I wrote this on a mac. It seems like the termios struct differs on linux and mac. Some of the constants might differ as well, so it's hard to guarantee. https://github.com/thatstoasty/termios/blob/main/examples/get_key.mojo I reimplemented the python's termios and tty functions by calling the underlying C functions via external_call in Mojo. https://github.com/thatstoasty/termios/blob/main/termios/c/terminal.mojo
MyriadColors
MyriadColorsOP8mo ago
THanks, I will take a look
bunny
bunny8mo ago
and if you get it working across platforms, perhaps contrib to std-lib input() function 😉
MyriadColors
MyriadColorsOP8mo ago
The code doesnt seem to be too complicated, problem is that Im not too familiar with posix I will se ewhat i can do
bunny
bunny8mo ago
maybe you two can tag-team it!
Want results from more Discord servers?
Add your server