Most real-world applications depend on user inputs and change their execution logic based on those inputs. Their outputs can depend on what the user inputs into the program. So far we’ve only looked at programs that print fixed outputs.
print() is used to print output. input(), on the other hand, is used to get input from the user. The user can enter their name, phone number, some message, etc.
So, if we would like to write a simple program that would greet the user we should store the user input in some variable and print it later (we will discuss variables in more detail later, but you can think of it as a container that can store data):
name = input()
Here, the program waits for the user to input a text and stores that value in a variable called name. The program then prints the fixed text Hi followed by the value stored in the variable called name. In case the user inputs Anna, the program would print Hi Anna. In case the user inputs Simon, the program would print Hi Simon. So, the same program prints different things depending on what the user inputs.
Note that name is assigned to input(). This tells Python to store the value entered by the user in the variable called name. After that, we are able to print the name like any other text or number. Pay attention that to print the value stored in the variable, we don’t use quotes and instead write the name of the variable.
Write a program that would greet the user with Hello, and then the name of the user.
Here are some examples of how the program should behave for different inputs:
In the first test case, the program receives an input Anna, and then prints Hello, Anna.
In the second test case, the program receives an input Bob, so it prints Hello, Bob.
In the third test case, the program receives an input Simon, so it prints Hello, Simon.
Note: The program will be executed separately for each test case (those are completely different program executions).