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






