MyriadColors
MyriadColors
Explore posts from servers
MModular
Created by MyriadColors on 5/15/2024 in #questions
How to initialize an empty List[String]
What Im trying to do:
from python import Python

fn add(a: Int, b: Int) -> Int:
return a + b

fn sub(a: Int, b: Int) -> Int:
return a - b

fn mul(a: Int, b: Int) -> Int:
return a * b

fn safe_div(a: Int, b: Int) -> Float32:
return a / b

fn main() raises:
var user_input = Python.evaluate("input('Enter [verb] [args]: ') ").split(" ")

var verb: String = ""
var args: List[String]

for i in range(0, len(user_input)):
if i == 0:
verb = user_input[0]
else:
args.append(user_input[i])
from python import Python

fn add(a: Int, b: Int) -> Int:
return a + b

fn sub(a: Int, b: Int) -> Int:
return a - b

fn mul(a: Int, b: Int) -> Int:
return a * b

fn safe_div(a: Int, b: Int) -> Float32:
return a / b

fn main() raises:
var user_input = Python.evaluate("input('Enter [verb] [args]: ') ").split(" ")

var verb: String = ""
var args: List[String]

for i in range(0, len(user_input)):
if i == 0:
verb = user_input[0]
else:
args.append(user_input[i])
Problem: when compiling: /home/pedrot/Desktop/Coding/mojo/learn/main.mojo:25:24: error: use of uninitialized value 'args' args.append(user_input[i]) ^ /home/pedrot/Desktop/Coding/mojo/learn/main.mojo:19:5: note: 'args' declared here var args: List[String] ^ mojo: error: failed to run the pass manager But when I try:
var args: List[String] = []
var args: List[String] = []
It also complains: /home/pedrot/Desktop/Coding/mojo/learn/main.mojo:19:30: error: cannot implicitly convert 'ListLiteral[]' value to 'List[String]' in 'var' initializer var args: List[String] = [] ^~ mojo: error: failed to parse the provided Mojo source module THank you.
4 replies
MModular
Created by MyriadColors on 5/15/2024 in #questions
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.
31 replies