liveBook · Manning

Input()

As explained in Python Workout, Second Edition

The input function reads a line of text from standard input, optionally displaying a prompt, and returns it as a string.

Use input() when a program needs information from a user. It displays an optional prompt and always returns the entered value as a string, so convert numeric responses before calculations or ordered comparisons. section 2.2 section 2.3

Number guessing loop

A guessing game combines input(), int(), comparisons, and a loop: number = random.randint(0, 100); while True: guess = int(input('Guess the number: ')); if guess < number: print('Too low'); elif guess > number: print('Too high'); else: print('Just right'); break. Without int(), comparisons between the entered string and integer fail. section 2.2 section 2.3

Handle interactive input with these practical patterns:

  • Convert numeric text immediately with int() or float() when later operations require numbers. section 2.3
  • Use while True with break when prompting repeatedly until a valid answer or successful result appears. section 2.3
  • Treat an empty response as false in a Boolean context; walrus assignment can combine reading and testing in a loop. section 2.3 section 5.9
  • Validate numeric text with try/except ValueError or a string check before storing it in a data structure. section 5.9
name = input('Enter your name: ')
print(f'Hello, {name}!')

A minimal prompt-and-response example shows input() supplying a name for an f-string greeting. section 2.2

try:
    mm_rain = int(input('Enter mm rain: '))
except ValueError:
    print('You didn't enter a valid integer; try again.')
    continue

rainfall[city_name] = rainfall.get(city_name, 0) + mm_rain

A complete validation pattern converts rainfall input, reports invalid integers, retries, and updates a dictionary. section 5.9

How Python Workout, Second Edition covers it

How deep it goes
Explains prompting, string results, conversion, repeated input, empty responses, and validation, but does not descend into input internals.
Examples
Uses a number guessing game, a name greeting, a repeated-thoughts loop, and rainfall collection with numeric validation.
What it assumes
Assumes basic Python functions, variables, conditionals, and loops while teaching user input, string results, and type conversion through exercises.
What you can do after
Build interactive Python programs that prompt users, convert responses, retry invalid input, and terminate after a successful condition.
How it connects
Connects input() to type conversion, loops, comparison operators, f-strings, exception handling, and dictionaries.

Also explained in

Compare how 7 books explain input() →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.