Input

Input is any information that the program recieves from the user while it is running. The program then uses this input to make decisions, perform calculations or display results.

Input allows programs to be interactive, meaning they can respond to what the user types.

Example 1 - Combining Input and Output

1firstName = input("What is your first name? ")
2
3print(f"Hello, {firstName}!")
Show Output
What is your first name? John
Hello, John!

Explanation

The first line uses the input() function to display the message “What is your first name?” on the screen. The program then pauses and waits for the user to type their answer. Whatever the user types is stored in the variable named firstName.

The third line uses the print() function to display a message that includes the name the user entered. The f before the quotation marks tells Python to insert the value of the variable firstName into the message.

Example 2 - Using Multiple Input Statements

1firstName = input("What is your first name? ")
2
3print(f"Hello, {firstName}!")
4
5favouriteColour = input("What is your favourite colour? ")
6
7print(f"Your favourite colour is {favouriteColour}!")
Show Output
What is your first name? Jane
Hello, Jane!
What is your favourite colour? blue
Your favourite colour is blue!

Explanation

The first line uses the input() function to ask the user to type in their first name. The program waits for the user to enter their response and then stores that text in the variable named firstName.

The third line displays a greeting that includes the name the user typed in. The f before the quotation marks allows the program to insert the value of the variable firstName into the message.

The fifth line asks the user another question - this time about their favourite colour. The user’s answer is stored in a new variable called favouriteColour.

The seventh line prints a message that repeats the colour the user entered.

Example 3 - Performing Calculations with User Input

1number1 = int(input("Enter a number: "))
2
3number2 = int(input("Enter another number: "))
4
5result = number1 + number2
6
7print(f"{number1} + {number2} = {result}")
Show Output
Enter a number: 10
Enter another number: 15
10 + 15 = 25

Explanation

The first line uses the input() function to ask the user to type in a number. The int() part converts the user’s input from a string into an integer, which allows the program to perform mathematical calculations. The number entered is then stored in the variable number1.

The third line does the same thing, but this time stores the second number in a variable called number2.

The fifth line adds the two numbers together and stores the total in a new variable called result.

The seventh line displays the full calculation and the answer on the screen.

Example 4 - Converting Kilometres to Metres

1kilometres = float(input("Enter a distance in kilometres: "))
2
3metres = kilometres * 1000
4
5print(f"{kilometres}km is {metres}m.")
Show Output
Enter a distance in kilometres: 2.5
2.5km is 2500.0m.

Explanation

The first line asks the user to type in a distance measured in kilometres. The input() function displays the message “Enter a distance in kilometres:” and waits for the user to respond. The float() part converts the user’s answer into a decimal number so that the program can handle distances that include fractions (for example, 1.5 km). The value entered is then stored in the variable kilometres.

The third line multiplies the number of kilometres by 1,000 to convert it into metres. The result of this calculation is stored in the variable metres.

The final line displays the conversion result in a clear sentence.