← Intro to programming with Python
Asking questions with input()
Let the user type an answer.
So far every program does the same thing every time. input() changes that: it shows a question, waits for the person to type an answer, and gives you that answer as a string. Store it in a variable and you can use it like any other value. This is what turns a fixed script into something interactive. (In these exercises, the answers are typed in for you automatically, so you can test your program.)
name = input("What is your name? ")
print(f"Nice to meet you, {name}!")If the person types Priya, the program shows Nice to meet you, Priya! The space at the end of the question keeps the answer from touching the question mark.
Common mistake: forgetting to store the answer. input("Name? ") on its own line asks the question but throws the answer away.
input() ALWAYS gives back a string, even when the person types a number. The next lesson shows how to fix that.