liveBook · Manning

Input

As explained in Get Programming: Learn to code with Python

Information supplied to a program or software component for processing.

Input is information a program or task receives before it does its work. Output is what it produces. For a minutes-to-hours converter, the minutes are input; the displayed hours and minutes are output. Identifying both before coding helps clarify the task. section 6.2 section 2.5

Input can reach a program in several ways:

  • A value can be set in code, such as minutes_to_convert = 123. section 6.2
  • A user can type a response when the program asks a question. section 11.2
  • A program can read structured data from a file when entering many values one by one would be impractical. section 23.0

Ask for a number

user_input = input("Enter a number to find the square of: ") num = int(user_input) print(num * num) First, input() displays the prompt and waits for the user to press Enter. The response is stored in user_input as a string, even if the user typed digits. Next, int() converts that string to an integer. Finally, the program prints the square. Storing the response matters: asking a question but ignoring its answer cannot change the result. section 11.2

Input can also guide which code runs. A decision can select statements based on a user's response. Make expected responses clear: an adventure game, for example, announces that commands must be in capital letters and that other commands end the program. section 13.0 section 15.1

A function receives input through parameters. In a definition such as def get_word_length(word1, word2), the parameter names stand in for values not yet supplied. Calling the function with two strings gives those parameters values, in order, for that call. The function can then combine the strings and return their length. A docstring can state the expected parameter names and types, what the function does, and what it returns. section 21.1 section 21.2 section 21.3

Inputs deserve testing, not just collecting. Before writing a name-mashup program, for example, plan to try short and long names, names with odd and even lengths, and other combinations. Run the program with different inputs and compare its actual output with the expected output; debug and repeat when they differ. section 12.1 section 2.3

How Get Programming: Learn to code with Python covers it

How deep it goes
Works through prompting, storing, converting, and testing inputs, then explains parameter values in function calls.
Examples
Uses a square calculator, a name mashup, function calls, and a file of names and phone numbers.
What it assumes
Teaches prompting from the first input() call while building on variables and later introducing function parameters.
What you can do after
Build it from scratch: prompt for and process user responses, supply function arguments, and test varied inputs.
How it connects
Connects input to output, variables, type conversion, decisions, functions, files, and testing.

Also explained in

Compare how 14 books explain input →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.