Python · Some intermediate topics
input()
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — What is input()?
In real Python programs, `input()` lets you ask the user to type something. The program pauses and waits for the user to respond. It's like asking a trainer: "Which Codémon do you choose?"
name = input("What's your name? ")
print("Hello, " + name + "!")
When this runs, Python displays "What's your name? " and waits. The user types something, presses Enter, and the value goes into `name`.
input() might not work here
Because of how this app works, `input()` may not function properly in the code editor.
Exercise 2 — The Prompt Message
You can pass a message (called a prompt) inside `input()` to tell the user what to type.
No message for input()
Due to app limitation, the message will not appear in the app unfortunately. Sorry about that!
codemon = input("Choose your Codémon: ")
level = input("Enter its level: ")
print(f"{codemon} is level {level}!")
The string you pass to `input()` is displayed to the user before they type. Without it, the user wouldn't know what to enter!
Exercise 3 — input() Always Returns a String
Whatever the user types, `input()` always gives you a string. Even if they type a number like 42, it's the string `"42"`, not the number `42`. You need to convert it.
age = input("Your age? ")
print(age, type(age))
age_number = int(age)
print(age, type(age_number))
print(age_number + 1)
See? `age` starts as a string. We use `int()` to convert it to a number so we can do math with it.
Exercise 4 — input() Return Type
What is the type of `x` after running `x = input("Enter a number: ")` and the user types `42`?
Options
• `int`
• `str`
• `float`
• `bool`
Hint : `input()` always returns the same type, no matter what the user types.
Exercise 5 — Converting Input
Complete the code to get a Codémon's level as a number and add 5 to it.
level = input("Level? ")
level_num = ____(level)
print(level_num + ____)
Options: int str 5 "5" float
Hint : You need to convert the string to a number type before doing math.
Exercise 6 — Converting on the Spot
A common pattern is to wrap `input()` directly with `int()` or `float()`. This converts the result immediately.
level = int(input("Enter level: "))
hp = float(input("Enter HP: "))
print(f"Level {level}, HP: {hp}")
This is shorter than converting on a separate line. `int(input(...))` means: get the input, then immediately convert it to an integer.
Error if conversion fails
If the user types something that can't be converted (like `"abc"`), this will raise an error. So it's good for simple cases, but for more complex input validation, you might want to convert on a separate line and handle errors.
Exercise 7 — Conversion Patterns
x = int(input("Number: "))
print(x + 10)
If the user types `5`, what is printed?
Options
• 510
• 15
• Error
• 5 + 10
Hint : `int()` converts the string to a number first, so `+` does addition, not concatenation.
Exercise 8 — Float Conversion
Complete the code to simulate getting a Codémon's weight as a decimal number.
weight = input("Weight? ")
weight_num = ____(weight)
print(weight_num + ____)
Options: float int 1.5 "1.5" str
Hint : For decimal numbers, you need `float()`, not `int()`.
Exercise 9 — input() Patterns
Match each `input()` pattern with what it returns.
Items to match
• `input("Name? ")`
• `int(input("Age? "))`
• `float(input("Price? "))`
• `input()`
Definitions
• Returns a string from user
• Returns an integer from user
• Returns a decimal from user
• Waits for input with no message
Exercise 10 — What Happens Without Conversion?
# User types "10"
x = input("Number: ")
print(x + x)
Options
• 20
• 1010
• Error
• 10 10
Hint : Without `int()`, the value is still a string. What does `+` do with strings?
Exercise 11 — Codémon Registration
Create a Codémon registration form!
1. Convert the user's input to the appropriate types
2. Calculate attack power as `level * 2`
3. Calculate defense as `hp // 10`
4. Print a summary like: `"Aquachu - Level 10 - HP: 100 - Attack: 20 - Defense: 10"`
Starter code
# Ask for inputs
name = input("Enter Codémon name: ")
level = input("Enter level: ")
hp = input("Enter HP: ")
# Convert to numbers
# Calculate attack and defense
# Print summary
Back to the course