Victor Jeman Academy
Python Fundamentals9 min

Getting input from the player

Read what the player types with input(), convert it to numbers, and survive the classic input errors.

What You'll Learn

  • Read what the player types with input()
  • Know that input() always hands you a string (text)
  • Turn that text into a number with int() and float()
  • Recognize and fix the classic input errors (TypeError, ValueError)

So far your programs have done all the talking. You hardcoded the hero's name and class in lesson 2, the program printed them back, and the player just watched. This lesson hands the keyboard to the player. They type their name and their age, and your code reads it and reacts.

The tool that makes this happen is input(). It pauses your program and waits for the user to type something. The catch is that whatever they type comes back as text, so this lesson is also where you turn that text into a number and learn to handle the errors that show up when you forget.

The input() function

Cum functioneaza input()
>>> nume = input("Cum te cheama? ")

The input() function stops the program and waits for the user to type something from the keyboard. When the user presses Enter, the text they typed is returned (handed back) as a result for your code to use.

[!WARNING] Playground reads input differently In the browser playground, input() pops up a small box for you to type your answer, then continues. On your own computer it reads from the keyboard instead. Either way, the code is the same.

When you run this code, the program displays "What is your name? " and then waits. If you type "Alex" and press Enter, you will see:

text
What is your name? Alex
Hello, Alex

The text between the quotes in input("...") is the message the user sees. Think of it as the question your program asks. You can put any text there, or leave the parentheses empty if you do not need a prompt:

Here is the part that trips up every beginner: input() always returns a string (a sequence of characters). Even when the user types 25, Python hands you the text "25", not the number. That detail causes the first real bug in this lesson, and you will hit it in a minute.

Strings

A string is text. You create one by putting characters between quotes. Single or double quotes both work the same way:

Pick whichever quote is more convenient. If your text contains an apostrophe, wrap it in double quotes so Python does not get confused about where the string ends:

Or the other way around, if the text contains double quotes:

Strings can be empty (no characters) or contain very long texts:

Type conversion

Conversia tipurilor
"14"
str
-->int()
14
int

This is the bug mentioned earlier. input() always returns a string, so when you ask the player their age, the answer comes back as text, not a number.

Why does that matter? Because you cannot do math on text:

To transform a string into a number, you use:

  • int() - converts to an integer (no decimals)
  • float() - converts to a decimal number

You can write everything on a single line by putting input() directly inside int():

Here is a full example where the program reads a number and does a calculation with it:

See the str() at the end? approx_age is a number, and you cannot glue a number onto a string with +. str() runs the conversion the other way: it turns a number back into text so + will accept it. You will lean on both directions a lot once you start building messages, which is the next lesson.

Common errors

Once you mix input() and conversions, you will hit a few classic errors. They look scary the first time. Here is what each one means and how to fix it.

TypeError - mixing different types:

Python will tell you: TypeError: can only concatenate str (not "int") to str. The fix: convert with int() before doing math.

ValueError - text that cannot be a number:

Python throws: ValueError: invalid literal for int() with base 10: 'hello'. This happens when the user types text where you expected a number. For now, just type a number when the program asks for one. Later, in the modules and errors lesson, you will learn to catch this with try/except so a wrong keystroke does not crash the whole game.

Surprise concatenation - numbers as strings:

This is one of the most common traps. Since input() returns strings, + joins them instead of adding them. The fix: convert both values to int() or float().

input() always hands you a string. Want to do math on it? Convert it first with int() or float().

Exercise : Counting potions

Ask the player two questions: how many potions they carry, and how much HP a single potion restores. Both answers come back from input() as text, so convert each one with int(). Multiply them to get the total healing available and display the result. Try typing a word instead of a number for one of the answers and watch the ValueError show up, that is the trap this lesson warned you about.

Exercise : Pick your weapon

Let the player choose a starting weapon by typing a number. Print the menu (1. Sword, 2. Bow, 3. Staff), read the choice with input(), and convert it with int(). Use if/elif/else to print the bonus for the chosen weapon. Add an else branch for any number that is not 1, 2, or 3.

Complete game code: Python Kingdom

Here is your game so far. The big change: the player now types the hero's name and picks a class while the game runs. Your if/elif/else branches react to that choice instead of a value you hardcoded.

The hero is finally the player's own. But every line that prints the stats is still glued together by hand. Next lesson you clean that up.

Where this goes next

You can now read what the player types and turn it into a number. But every message you printed back was glued together by hand with +, a pile of quotes, and the occasional str(). There is a cleaner way to build those messages, and a clean way to branch on the player's choice. That is the next lesson: Building strings.

Test Your Knowledge

Check how well you understood the lesson with these 3 questions.

Question 1 of 3

What data type does the input() function return?