Metadata
Title
5: Input
Category
general
UUID
f4bbe6a2f5f5435ba2a881b74d6bcefe
Source URL
https://cscircles.cemc.uwaterloo.ca/5-input/
Parent URL
https://cscircles.cemc.uwaterloo.ca/
Crawl Time
2026-03-18T05:13:25+00:00
Rendered Raw Markdown

5: Input

Source: https://cscircles.cemc.uwaterloo.ca/5-input/ Parent: https://cscircles.cemc.uwaterloo.ca/

In the last lesson we discussed user input, but did not really explain how user input is obtained. In Python, the user types one line of input at a time. You should use the input() function to actually obtain the next line of input from the user. The input() function takes no arguments and always gives back a str.

On this website, all input must be specified before the program runs. If you run Python interactively at home then input() actually pauses the program and waits until the user types a line of text. Also, when working interactively, you can take advantage of the fact that input() accepts an optional string input, which will be interpreted as a prompt for the user. E.g.,
number = input("Enter a number between 0 and 100. ")

Here is an example of using input() to get input. The grader will automatically specify the input for the program.

Example

Echoing a line of text

line = input() print("The first line of input is:", line)

The next example demonstrates:

Example

inputString = input() aFloat = float(inputString) print(aFloat + 1) print('Second line:', input())

You may enter input for the program in the box below.

From now on, most exercises allow the option of entering your own test input. Try the following experiment: press the Enter input button above. Leave the input text box empty. Then, press Run test. You should get an error like

EOFError: EOF when reading a line

The acronym EOF stands for End Of File. This message literally means that the program called input() but failed to have any available input to read.

For the next exercise, you are asked to debug a program which is not working, and make it work. Note that the bug is not a typo, but rather a logical bug: the program was not correctly designed to do its job, so you must redesign it a bit.

Coding Exercise: Echo

Write a program that reads one line of input, and prints out that same line two times. For example, if the input is Echo the output should be

Echo\
Echo

Fix the broken sample solution given below. (Or, delete the whole sample solution and start from scratch.)  Hint

You need to create an account and log in to ask a question.

print(input()) print(input())

You may enter input for the program in the box below.

More actions... History Reset code to default Help

You can continue to the next lesson, which is about if statements.