Python’s input function returns text. A program that expects a number must convert that text and handle the possibility that conversion fails.
Worked example
If a player enters 12, the program initially receives the string containing those two characters. Converting it with int produces an integer suitable for addition. Without conversion, combining it with another string joins text. The strings 12 and 3 form 123 when concatenated, while the integers twelve and three sum to fifteen.
What to check
Try a valid number, an empty answer, and a word. int raises ValueError when the text does not represent an accepted integer. A small try and except block can display a clear message and ask again. Decide whether negative numbers are allowed after conversion, because a valid integer can still violate the rules of your game. Keep format validation and game-rule validation distinct.
Continue with the book
Explore the full treatment, worked examples, and exercises in Code Quest with Python.
Companion article for Code Quest with Python, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
