We’ve been using the print function to print text, like this:
Functions are named blocks of code that achieve a particular task. In this case, we are running the print function, which outputs text to the console or terminal.
We have passed the string “Hello” to the function; things that we pass to functions are called arguments.
In the lingo, we say that we have called the print function, passing it a string argument.
You can think of functions as black boxes that we can pass data to. Functions can also return data; or in other words, they can give us data back.
Now let’s take a look at another function that enables us to make our programs interactive.
The Input Function
Take a look at this program. It asks the user to enter some text and then tells the user what they’ve entered.
text = input("Enter some text > ") print("You entered:", text)
Enter some text > hi there You entered: hi there
The input function accepts a string argument, which it prints on the console. This is intended as a prompt to the user to type something.
The function then waits for the user to enter some text, which it returns.
The return value of the function (the text the user enters) is then assigned to the variable called ‘text’.
Finally we call the print function with two string arguments: the text “You entered:” and the text the user actually entered. These both get printed, separated by a space.
Finally we can begin to write interactive programs!
Leave a Reply