Input/output
input() is a built-in function that pauses a running program and waits for the user. Everything stops at that line — the next line does not run until the user types a response and presses Enter. When Enter is pressed, the function returns the text that was typed.
That returned value is stored in a variable using =. The variable holds the response for the rest of the program, so you can display it or join it to other text later on. If you do not assign the answer to a variable, it is thrown away and cannot be used again.
sport = input("Which sport do you play? ")
print("Great - " + sport + " is a good choice.")Output when the user types Hockey:
Which sport do you play? Hockey
Great - Hockey is a good choice.
A program can call input() as many times as it needs. Each call pauses the program at its own line, so answers are collected one at a time, in the order the statements appear. Give every answer its own variable to keep the responses apart — storing a second answer in the same variable would overwrite the first, and that text would be lost.
starter = input("Which starter would you like? ")
main = input("Which main course would you like? ")
print("Order: " + starter + " then " + main + ".")Output when the user types Soup, Pasta:
Which starter would you like? Soup
Which main course would you like? Pasta
Order: Soup then Pasta.
Whatever the user types comes back as text (a string), even digits such as 7, so it can be displayed or joined to other text straight away but cannot yet be used as a number.
Collecting a value with input() is only half the job. A program should do something useful with what the user typed, then show a result that clearly connects to that answer, so the user can see their own value being worked on.
Processing means using the stored value in a calculation or other operation to create something new. Responding means printing a message that presents that new result, and repeating the user's own figure where it helps them check the program used it.
A student has a daily screen time limit of 120 minutes. The program asks how many of those minutes have been used so far, takes the used minutes away from the limit, and reports what is left.
# daily screen time limit is 120 minutes
used = int(input("How many minutes of screen time have you used so far? "))
left = 120 - used
print("You have " + str(left) + " minutes of screen time left.")Output when the user types 45:
How many minutes of screen time have you used so far? 45
You have 75 minutes of screen time left.
The prompt asks for a number, int() turns the typed text into a whole number (Text Input and Casting covers this in more detail), 120 - used carries out the processing, and str() joins the answer into the sentence so it is displayed as part of a message rather than as a bare number.
A reply of just "75" leaves the user guessing what was worked out. "You have 75 minutes of screen time left" names what was calculated and gives the unit, so the response makes sense on its own. Good processing and a clear, relevant message turn a typed answer into something the user can actually use.