The Input Function

In the past few lessons I showed how we can tell a computer program to send output (printed text) to the standard output. However, as previously mentioned, another critical role of a program is to be able to accept input from a user. One of the simplest ways to get input from a user in Python is via the input function. Using this function, we can ask the user to give us strings that we can then use in our program. In order to get input from the user, we will have call the input function, then store the string that the user gives us into a variable, then we can use this variable elsewhere in the code (for example, print it out!).

Calling the input function is similar to the print function. For example:

input('Type an address: ')

If you were to type this in to your IDE and hit the run button, it would print out the text Type an address as the print function would. However, after that, it waits for you, the user, to type in text on the console. You can type in whatever text you want, and when you are finished, you can press ENTER (RETURN) to continue. For example:

IDE example

However, it is not very useful unless we actually do something with the value that is entered. We say that the input function returns the string that the user types in. Therefore, we need to put a variable = before the call to the function, in order to save the string. This variable will refer to whatever the user typed, and then it can be used! For example:

IDE example

Recall the input and output diagram that I showed a few lessons back. We now have arrived at a very basic program that has both input and output.

graph LR classDef blue fill:#cef,stroke:#059; I[get address from user] --> P{Python Program} P --> O[Print out the address] class I blue; class P blue; class O blue;

Remember the script example from the previous lesson, where we had two characters stores as variables:

character_1 = 'James'
character_2 = 'Luke'
print(character_1 + ': Hi ' + character_2 + ', long time since we talked last.')
print(character_2 + ': Hi ' + character_1 + '! How have you been?')
print(character_1 + ': I have been well. Do you wanna go get lunch?')
print(character_2 + ': Yes!')
print(character_1 + ': Great, we can meet at In-n-Out burger next week')

Using variables made it easier to change the name of a character, especially if we had had a much longer script. However, what if we wanted this script to not have fixed character names. Rather, we could have the user choose what the names of the two characters are, so it could be customizable depending on the user preference. We could change the first two lines so that the program requests inputs, rather than having fixed names:

character_1 = input('Name for character 1: ')
character_2 = input('Name for character 2: ')
print(character_1 + ': Hi ' + character_2 + ', long time since we talked last.')
print(character_2 + ': Hi ' + character_1 + '! How have you been?')
print(character_1 + ': I have been well. Do you wanna go get lunch?')
print(character_2 + ': Yes!')
print(character_1 + ': Great, we can meet at In-n-Out burger next week')

Try running this in your IDE a few times, each with different names. You should be able to see that the names of the character in the script changes based on what names you type in.

What type of value does the input() function return? A Number Incorrect. The input() function returns a string. A String Correct!

As another simple example, shown below is a program that would print out some text that looks like a Christmas tree:

print('  *')
print(' ***')
print('*****')
print('  |  ')

We could make this program more interesting by allowing the user to customize the way the tree appears. We could instead write the program as so:

letter = input('Type a letter to use for the Christmas tree: ')
print('  ' + letter)
print(' ' + letter + letter + letter)
print(letter + letter + letter + letter + letter)
print('  |  ')

Try it out for yourself. There are many uses for asking for an input value from a user, and what you should ask for depends on what the purpose of the program is. A few example include:



PyFlo Home Complete Bookmark Next