Python input(): Get Input from the User
What Is input()?
So far, your programs always did the same thing. Now they'll listen. The built-in input() function lets the user type something, and gives it to your program:
name = input()
print("Hello,", name)Hello, Sunny
input()waits for the user to type a line.- Whatever they type is stored in
name. - The next line uses it, like any other variable.
Change the name in the Input box and watch the output change. The same program now greets anyone.
Think of input() as a waiter taking your order: the program pauses, asks, writes your answer down, and carries on.
How input works on LivePython
On your own computer, Python stops and waits while you type. On LivePython, Python runs in your browser, so you type the answers first, in the Input box, one per line. Then the code runs and reads them in order. It's the same input(), just with the answers ready in advance.
Remember chapter 4?
When you learned variables, you saw that one variable lets a program work for any name. input() is where that pays off: the value comes from the user, not from your code.
input() reads one line the user types, and gives it to your program.