So far, our programs have been very self-contained. They've never stopped to ask
the user running them what he/she wants. It's about time we changed that. To do
that, we'll be using the input
function. The example below will ask you for
your name using a dialog box. Please don't be alarmed by it. Go ahead and hit
the Run button below.
If you ran the code above, you know that the program causes a dialog to pop up
that asks you for your name. Once you enter some text in the input field of the
dialog and hit "OK", the input
function "returns" the text you entered as
a string. We assign that text to a variable (name
in this case) and later
make use of it in a print
.
In future lessons, we'll go over this concept of functions "returning" stuff.
Until then, you'll just have to remember that the syntax for accepting user
input is destination_variable = input("Message to user")
.
Feel free to explore the input
function above by trying various alterations.
Can you add a second input to ask for the user's age and print that out as well?
© 2024 Abhin Chhabra | Sitemap