Learn to receive data from the user with input() and work with strings.
Up until now, your programs have been a bit... lonely. You told them what to do, they listened, and that was it. But today everything changes. You will learn how to make your program ask questions and respond based on what the user says. Basically, you will build the first steps toward a chatbot.
Imagine a game where the computer asks your name, your age, and then tells you something personalized. Or a text adventure where you choose what happens next. It all starts with one simple function: input().
Up until now, the hero in the Python Kingdom had a fixed name and class. It is time to let the player choose! In this lesson you make character creation interactive using input(), display stats with f-strings, and build a shop where the player types what they want to buy.
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 as a result.
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:
What is your name? Alex
Hello, AlexThe text between quotes in input("...") is the message displayed to the user. It is like a question your chatbot asks. You can put any text you want there, or you can leave the parentheses empty if you do not want to display anything:
One very important thing: input() always returns a string (a sequence of characters). Even if the user types a number, Python treats it as text. We will see why this matters very soon.
A string is simply text. In Python, you create a string by putting text between quotes. You can use single or double quotes, both work identically:
You choose between single and double quotes based on what is more practical. For example, if your text contains an apostrophe, you use double quotes to avoid issues:
Or the other way around, if the text contains double quotes:
Strings can be empty (no characters) or contain very long texts:
Concatenation means joining two or more strings together. In Python, you do this with the + operator:
Notice that Python does not automatically add a space between them. If you want a space, you must add it yourself:
You can combine concatenation with input() to build personalized messages:
If the player types "Andrew" and "Mage", they will see:
Hero Andrew is a legendary Mage!You can also multiply a string by a number to repeat it:
Here is where things get interesting. Remember: input() always returns a string. This means that if you ask the user how old they are, the answer is text, not a number.
Why does it matter? Because you cannot do math with text:
To transform a string into a number, you use:
int() - converts to an integer (no decimals)float() - converts to a decimal numberYou can write everything on a single line by putting input() directly inside int():
Let's see a complete example where our chatbot does a calculation:
Notice that we also used str() at the end. Why? Because approx_age is a number and you cannot concatenate a number with a string. The str() function does the reverse conversion: it turns a number into text.
When working with input() and conversions, you will encounter some classic errors. Let's look at them so you know how to fix them.
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 will throw: ValueError: invalid literal for int() with base 10: 'hello'. This happens when the user types text instead of a number. For now, make sure you type a number when the program asks for one. In a future lesson you will learn how to handle this situation gracefully.
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().
Concatenation with + works, but it can get clunky when you have many variables. Python offers a much more elegant method: f-strings (formatted strings).
An f-string starts with the letter f before the quotes, and you put variables inside curly braces {}:
Result: Player Andrew has reached level 7!
Compare the two approaches:
In f-strings you can put expressions too, not just variables:
Let's combine everything we learned into a mini chatbot:
The \n character in the f-string creates a new line, making the text more airy and easier to read.
The input() function always returns a string. If you want to do math, convert with int() or float(). And for beautiful messages, f-strings are your best friend.
Ask the player to enter their hero's name using input(). Then display a personalized welcome message: "Welcome to the Python Kingdom, [name]! Your adventure begins now."
READ hero_name from user with the message "What is your hero's name? "
DISPLAY "Welcome to the Python Kingdom, " + hero_name + "!"
DISPLAY "Your adventure begins now."Display the 3 available classes (1. Warrior, 2. Mage, 3. Archer) and ask the player to choose by typing 1, 2, or 3. Use if/elif/else to set the variable hero_class to the correct value. If the player types something else, set the class to "Warrior" as a default value. Display the chosen class.
DISPLAY "Choose a class:"
DISPLAY "1. Warrior 2. Mage 3. Archer"
READ class_choice from user
IF class_choice == "1":
SET hero_class = "Warrior"
ELSE IF class_choice == "2":
SET hero_class = "Mage"
ELSE IF class_choice == "3":
SET hero_class = "Archer"
ELSE:
SET hero_class = "Warrior"
DISPLAY "Invalid choice. You have been assigned Warrior."
DISPLAY "Your class: " + hero_classAfter the player has chosen their name and class, display a nicely formatted character sheet using f-strings. The sheet must include: name, class, HP, attack, defense, and gold. Use = to create a frame and align the values. The class bonuses are from lesson 2 (Warrior: +20 HP, +5 attack; Mage: +5 HP, +10 attack; Archer: +7 attack, +5 defense).
READ hero_name from user
READ class_choice (1/2/3)
SET hp = 100, attack = 15, defense = 10, gold = 50
IF class_choice == "1":
SET hero_class = "Warrior", hp = hp + 20, attack = attack + 5
ELSE IF class_choice == "2":
SET hero_class = "Mage", hp = hp + 5, attack = attack + 10
ELSE:
SET hero_class = "Archer", attack = attack + 7, defense = defense + 5
DISPLAY "===== HERO SHEET ====="
DISPLAY " Name: {hero_name}"
DISPLAY " Class: {hero_class}"
DISPLAY " HP: {hp}"
DISPLAY " Attack: {attack}"
DISPLAY " Defense: {defense}"
DISPLAY " Gold: {gold}"Create an interactive shop where the player chooses what to buy. Display 3 items with prices: Iron Sword (30 gold, +8 attack), Wooden Shield (25 gold, +5 defense), HP Potion (15 gold, +20 HP). Ask the player to choose (1/2/3/4 where 4 = buy nothing). Check if they have enough gold, apply the bonus, and display the corresponding message with f-strings.
SET gold = 50, attack = 20, defense = 10, hp = 120
DISPLAY "--- Kingdom Shop ---"
DISPLAY "1. Iron Sword - 30 gold | +8 attack"
DISPLAY "2. Wooden Shield - 25 gold | +5 defense"
DISPLAY "3. HP Potion - 15 gold | +20 HP"
DISPLAY "4. Buy nothing"
READ shop_choice from user
IF shop_choice == "1":
IF gold >= 30:
gold = gold - 30
attack = attack + 8
DISPLAY "You bought the Iron Sword! Attack: {attack}, Gold: {gold}"
ELSE:
DISPLAY "Not enough gold! You are missing {30 - gold} coins."
... (similar for 2 and 3)Build a temperature converter that works in both directions. The program first asks the user what type of conversion they want (type "1" for Celsius to Fahrenheit or "2" for Fahrenheit to Celsius), then asks for the temperature and displays the result.
The formulas are:
F = C * 9/5 + 32C = (F - 32) * 5/9Display the result with a descriptive message using f-strings.
DISPLAY "1 - Celsius -> Fahrenheit"
DISPLAY "2 - Fahrenheit -> Celsius"
READ choice from user
IF choice == "1":
READ celsius (converted to float)
SET fahrenheit = celsius * 9/5 + 32
DISPLAY "{celsius} C = {fahrenheit} F"
ELSE IF choice == "2":
READ fahrenheit (converted to float)
SET celsius = (fahrenheit - 32) * 5/9
DISPLAY "{fahrenheit} F = {celsius} C"
ELSE:
DISPLAY "Invalid choice!"Write a program that asks the user for the bill amount and the desired tip percentage. Calculate the tip and the total to pay. Display the result nicely formatted with f-strings. For example, for a bill of 85 dollars and a 15% tip: "Bill: 85.0 dollars, Tip (15%): 12.75 dollars, Total: 97.75 dollars".
READ bill (converted to float)
READ tip_percent (converted to float)
SET tip = bill * tip_percent / 100
SET total = bill + tip
DISPLAY "Bill: {bill} dollars"
DISPLAY "Tip ({tip_percent}%): {tip} dollars"
DISPLAY "Total: {total} dollars"Now your game has interactive character creation! The player chooses their name and class, sees stats with f-strings, and can buy from the shop:
Check how well you understood the lesson with these 5 questions.