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 -------------------------------------- .. code-block:: python :linenos: firstName = input("What is your first name? ") print(f"Hello, {firstName}!") .. dropdown:: Show Output | What is your first name? John | Hello, John! .. admonition:: 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 ------------------------------------------- .. code-block:: python :linenos: firstName = input("What is your first name? ") print(f"Hello, {firstName}!") favouriteColour = input("What is your favourite colour? ") print(f"Your favourite colour is {favouriteColour}!") .. dropdown:: Show Output | What is your first name? Jane | Hello, Jane! | What is your favourite colour? blue | Your favourite colour is blue! .. admonition:: 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 --------------------------------------------------- .. code-block:: python :linenos: number1 = int(input("Enter a number: ")) number2 = int(input("Enter another number: ")) result = number1 + number2 print(f"{number1} + {number2} = {result}") .. dropdown:: Show Output | Enter a number: 10 | Enter another number: 15 | 10 + 15 = 25 .. admonition:: 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 ------------------------------------------- .. code-block:: python :linenos: kilometres = float(input("Enter a distance in kilometres: ")) metres = kilometres * 1000 print(f"{kilometres}km is {metres}m.") .. dropdown:: Show Output | Enter a distance in kilometres: 2.5 | 2.5km is 2500.0m. .. admonition:: 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.