Python input() and print() for Games
How print() and input() work in Python, explained with tiny games: f-strings, turning answers into numbers, and cleaning what the player types.
Every console game is a conversation. print() is the game talking, and input() is the game listening. Master these two and you can build quizzes, story games and guessing games.
print(): show anything
print() writes text on a new line. Give it several values separated by commas and it puts spaces between them. An f-string (a string with f in front) lets you drop variables straight into the text inside curly brackets.
input(): ask the player
input("question ") shows the question, waits for the player to press Enter and gives back what they typed as text. Press Run and answer in the box that appears.
Numbers from input()
Because input() always returns text, "5" + 1 fails. Turn the answer into a number with int(), but check it first with .isdigit() so a typo doesn't crash the game.
Clean what players type
.strip()removes spaces at the ends (people add them by accident)..lower()means YES, Yes and yes all match"yes".- Wrap the question in a
whileloop to keep asking until the answer is valid.
💡 Common mistake: comparing input() with a number, like if answer == 5. It's never true, because the answer is the text "5".
Games that use input() and print()
Practise this with 3 of the 20 games:
- #05☆☆☆
Number Guessing Game in Python
Learn The random module, while loops and if, elif and else.
- #07☆☆☆
Mad Libs-Style Story Game in Python
Learn input() and print(), Strings and Variables.
- #16☆☆☆
Coin Flip in Python
Learn The random module, for loops and Variables.
Frequently asked questions
Why does input() return a string in Python?
The keyboard sends characters, so Python gives you exactly what was typed. You decide whether to turn it into a number with int() or float().
How do I print without a new line?
Use print("text", end=""). The end argument replaces the usual line break.