Menu

Input Function

Input Function

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.

Practise this