In this video we are going to create a very simple calculator for Mojo that requires a user input. So let's see how we can do that with Mojo. First of all, I have to say that Mojo is not a finished project yet. So several things need to be done with workarounds and a calculator would only be be helpful if we can get a user input. And a lot of other things are available in Python.
So we say from Python import Python as imported Python, which seems a little bit crazy as a statement, but actually if you hover over it, you see that it provides methods that help you use Python code in Mojo. So often if you don't understand something, it might be helpful to hover with your mouse cursor over the part that you want to know more about. For example, if you do that with main you will see that it is a function and it raises. Raises stands for it can raise an error. But back to topic. Now that we have imported the Python library, we start our very simple calculator with a print output.
Print will output a text. It says it prints a string, but actually it's going to print it down below here in the terminal output. If you don't see the terminal, you can click on View Terminal in Visual Studio code. So basically this is already a working program. We need to save it.
That can also be done by pressing CTRL and S. And below here in the terminal I can now run the file and it says give me a number. To actually get the user input, we need to create one and that is done by using the imported Python module from here. This one is called built ins. And when you delete the last part and press the key for the dot, you will see everything that can be imported.
We need dot input and now we can assign that input to a variable called my number. In this case I'm using LET. LET stands for a variable that is not changed. Actually I could use VAR for variable and if I do that it will give us a warning below that the value was never changed. So let's add another print statement below that will output you have given me the number and the number that we have typed in. Don't forget to save the file.
Let's clear the output of the terminal below. And now I will run Mojo Simple calculator.mojo.
Now it says we have an unknown declaration input. So let's add that here and say our input equals the imported builtins input from the user. Save the file again and when we clear the terminal and rerun it, it actually says give me a number. Now you see that it also says that my number was declared as a VAR but never mutated. Consider switching it to LET.
This is a warning, it's not actually an error. And if you want to get rid of it, just use LET instead of VAR. Save the file again, restart the program, and this time the warning is gone. It says, Give me a number. So let's say nine.
And now we get the answer. You have given me the number nine. So in this little video, you have learned how to import functions from Python, create a user input, and assign the user input to a variable that can be printed out in the terminal output. Of course, this was only the very first part of our calculator app. For now, I say thank you for watching, and I will see you in the next video.
Auf dieser Seite können Sie das Online-Video LEARN MOJO TUTORIAL - 13 HOW TO ENABLE USER INPUT AND IMPORT FROM PYTHON mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Learn Mojo Tutorial - Simple AI Tutorial 01 Januar 1970 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 200 Mal angesehen und es wurde von 4 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!