Handling User Input & Type Conversion

Interactive programs often need to accept user input. This lesson teaches how to safely convert text input to numbers and use them in calculations.

The input() Function

The input() function always returns user input as a string. There are two ways to convert to numbers:

Method 1: Convert after input

text_input = input("Enter number: ")
num = int(text_input)

Method 2: Convert during input

num = int(input("Enter number: "))
In these examples:
  • Both methods achieve the same result
  • Method 2 is more concise for immediate conversion
  • Method 1 is better if you need the original string
The starter code uses Method 2 - conversion happens right in the input line.

Conversion Functions

# Convert to integer
int("42") → 42
# Convert to float
float("3.14") → 3.14

Important: Conversion will fail with ValueError if the string doesn't represent a valid number.

Practical Example: Age Calculator

birth_year = input("Enter birth year: ")
current_year = 2024
age = current_year - int(birth_year)
print(f"You are approximately {age} years old")
Enter birth year: [User types 1990]
You are approximately 34 years old

In this example:

  • input() captures user input as string
  • int() converts to numerical year
  • Arithmetic works with converted value
  • F-string formats the output message
Note: This will crash if user enters non-numeric input - we'll handle errors in later lessons.

Common Pitfalls

# Forgetting conversion
num = input("Enter number: ") # Returns string
print(num * 2) # "5" * 2 → "55"
55

Always convert before numerical operations. Use type()to check variable types during debugging.

Real-world Use: BMI Calculator

weight = float(input("Enter weight (kg): "))
height = float(input("Enter height (m): "))
bmi = weight / (height ** 2)
print(f"Your BMI: {bmi:.1f}")
Enter weight (kg): 68
Enter height (m): 1.75
Your BMI: 22.2

Shows how multiple conversions and float operations create real-world calculations.

Your Task: Number Squaring Program

The starter code needs to:

  1. Get numerical input using input()
  2. Convert it to integer with int()
  3. Calculate the square (number multiplied by itself)
  4. Store result in square

Example: If user enters 5, output should be"The square of 5 is 25".

Section 1/6The input() Function

The input() Function

The input() function always returns user input as a string. There are two ways to convert to numbers:

Handling User Input & Type Conversion

Interactive programs often need to accept user input. This lesson teaches how to safely convert text input to numbers and use them in calculations.

The input() Function

The input() function always returns user input as a string. There are two ways to convert to numbers:

Method 1: Convert after input

text_input = input("Enter number: ")
num = int(text_input)

Method 2: Convert during input

num = int(input("Enter number: "))
In these examples:
  • Both methods achieve the same result
  • Method 2 is more concise for immediate conversion
  • Method 1 is better if you need the original string
The starter code uses Method 2 - conversion happens right in the input line.

Output:

Click "Check" to run your code.