How to use input() in an online Python compiler
Reviewed
In Python Studio, put one response per line in the Input tab before running your program. Each input() call reads the next line as a string. Use int() or float() to convert numeric responses. Output displays prompts and results; it is not an interactive typing terminal.
Open this example in the editorOpens as a new program; running code requires JavaScript.
How do I supply multiple input values?
Python's input() reads a line and returns its text without the trailing newline. Python Studio connects prepared text from the Input tab to standard input for each run. Type responses there, not into Output.
This example asks for a name and an age. The first line of prepared input answers the first call, and the second line answers the second call. Open the example to create a new file with both code and input already filled in.
- 01Open the input example as a new program.
- 02Select Input in the console and enter Ada on the first line and 36 on the second line.
- 03Run the program. The prepared responses are read in order.
- 04Change the input, then run again to start reading from the first line.
name = input("Your name: ")
age = int(input("Your age: "))
print(f"\nHi {name}! In 10 years you'll be {age + 10}.")Your name: Your age: Hi Ada! In 10 years you'll be 46.
Why do the prompts appear together?
input(prompt) writes the prompt without a newline. A normal terminal may echo the characters you type, but the Output panel does not echo prepared standard input. Two prompts can therefore appear on the same line even when the program reads two separate responses correctly.
Add an explicit print() if you want a visible line break. Keep the actual responses in Input. The text of a prompt does not determine how input is parsed: input() always returns a string.
How do I read numbers or space-separated values?
Use int(input()) for a whole number or float(input()) for a decimal. Adding strings joins them: the string '36' plus the string '10' becomes '3610', not 46. Convert before performing arithmetic.
For two numbers on one line, split the line into words and convert each word. The example below expects exactly two values, such as 3 4, on the first input line. It is an additional snippet to copy into a new tab; the main example link opens the name-and-age program.
a, b = map(int, input().split())
print(a + b)7
Why am I getting EOFError?
EOFError means input() tried to read beyond the available prepared input. Count the calls your program actually reaches, including calls inside loops, then provide enough lines in Input.
An empty line and no remaining input are different cases. An empty line can return an empty string; when no line is available, input() raises EOFError. Check the Input tab associated with the active file, since each saved program has its own input text.
How do I handle invalid numeric input?
ValueError means a conversion received text it could not interpret, for example int('Ada') or int('3.5'). Confirm the responses are in the right order, and use float() if decimals are allowed.
For a learning program, handle the specific exception and explain the expected format. A retry loop also needs additional prepared input lines; it cannot wait for you to type into Output.
try:
age = int(input("Age: "))
print(age + 10)
except ValueError:
print("Please enter a whole number.")