Input and Assign to Variable
Let’s see how we can take input from a user and save it for later use.
Taking input from a user
Let’s continue making our way into Python and the project. Notice that project requirements 1 and 5 are categorically different from requirement 2, which is printing something on the screen.
Now we know how to print a name on the screen using Python, but how does our code ask the elementary school student to tell us their name?
The built-in input() function
Just like print prints something on the screen, what would be the name of the built-in function that takes input from the user’s keyboard? You guessed it! Python offers us another built-in function with the name of input.
Note: After clicking the “Run” button, click anywhere inside the black terminal window. When you see a blinking cursor, you will know that the program is waiting for your input. Use your keyboard to write anything and then press enter. Give it a shot:
input()
Notice there’s only one line of code, and it says input(). You ran the code, and then, you typed in something. While you were typing, of course, you could see on the black command line, each letter that you pressed on the keyboard. But as soon as you hit the “
How does the user know what they are required to input?
From the end user’s perspective (in this case, a student), how are they supposed to know if they have to enter a name, a number, or anything else? Wouldn’t it be better if every time we want a user to input something, we could prompt them with an appropriate message or a question? Python lets us do this using the same
input("Please tell us your name: ")This is much better in terms of meaningful user interaction. This means, input() is a built-in function that can take an optional
It’s always a good idea to think about your code’s end user to make our apps more efficient for their use.
Where has the user’s input gone?
Now, the user knows what the code wants them to input, that’s one issue out of the way. But what about the input from the user? We need to be able to welcome, by name, any student who is using our app. Can we use the print() function in tandem with our newly learned input() function?
print(input("Please tell us your name: "))We have only taken the previous code and placed it neatly inside the print() function. When we run the code, it first takes input from our keyboard and prints whatever name we entered. This means that the input isn’t getting lost. The input() function does return the input as a text message that gets passed to the print() function to output on the screen.
This gives us great power because now our code is dynamic. We don’t have to write the name of the student inside our code’s print statement just to be able to fulfill project requirement 2. Instead of writing a separate app for Alex, Betty, Catherine, Dania, and so on, we have a code that can, in a single line, welcome anyone in a personalized way.
Let’s imagine the following interaction between our app and its user, Alex:
Hi Alex! Welcome to the math quiz.Alex, can you answer the following questions?.....Congratulations Alex, on scoring 5 marks!
Using our current (print(input())) scheme of things, we can surely pull off the first line, but how can we print the second and the last personalized output on the screen inside one program? We don’t want to write a Python code that only works for Alex alone. And, in case we ask the user to tell us their name three times in a single session, they will not appreciate such a forgetful app.
Variable assignment
We now know that input() returns the user’s response from the keyboard, and instead of passing that toward the print() function, Python also lets us save the user’s response in the computer’s memory. Python lets us name that memory. The programming community calls this phenomenon a variable. We can assign a value to a variable (using =), and finally, we can use this variable however many times in our code. For example, we can assign the value "Alex" to a variable user_name:
user_name = "Alex"
Choosing the name of the variable is the programmer’s choice. Our variable names must be meaningful so that anyone reading our code can easily understand what the code intends to do.
How do we know what is the value residing inside our variable? Surely, we can print the following:
user_name = "Alex" print(user_name)
Variables are case-sensitive in Python, i.e., it makes a difference to Python if the letters of your variables are in upper or lower case. Whatever choice we make, it is important to be consistent throughout our code wherever that variable is used. Secondly, the variable will always come on the left-hand side of the assignment operator =.
The fulfilled project requirement
Now, we can use our variable as many times in our code, as required by our project:
user_name = "Alex"
print("Hi", user_name, "! Welcome to the math quiz.")
print(user_name, ", can you answer the following questions?")
print("Congratulations", user_name, ", on scoring 5 marks!")Exercise
Of course, we don’t want only Alex to use our app. Can you slightly change the following code so that any user in the world can write their name in the app, and for the app to print the same messages for that user as well? Your challenge is to only change the first line of code, and the rest of the code should work automatically!
user_name = "Alex"
print("Hi", user_name, "! Welcome to the math quiz.")
print(user_name, ", can you answer the following questions?")
print("Congratulations", user_name, ", on scoring 5 marks!")In this lesson, we learned how to take input in our program and even store it inside a variable so we can use it anywhere else in the program.