Module 4: User Input Practice

Let's practice interacting with the user and handling their input!

Anatomy of Getting Input

The `input()` function lets your program ask the user for information. Click the parts below to identify them.
Focus: Interaction.

user_name = input("Enter your name: " )

Identify all 6 parts!

Input & Output Flow

See how `input()` works with `print()`. Write code to ask for a favorite color, then print a message using it. Enter a sample color in the "Simulated Input" box *before* running.

Output appears here...

The Type Mystery

`input()` always gives back text (a string), even if the user types numbers! This can cause surprises. Predict the outcome for each case below.
Focus: Data Manipulation (Types).

Case 1: Adding Numbers?

Imagine the user enters '5' when asked for `num1` and '3' for `num2`.

num1_str = input("Enter first number: ") num2_str = input("Enter second number: ") result = num1_str + num2_str print(result)

What will be printed?

Case 2: The Fix with `int()`

Now we use `int()` to convert the input. User enters '10' then '4'.

num1_str = input("Enter first number: ") num2_str = input("Enter second number: ") num1_int = int(num1_str) # Convert to integer num2_int = int(num2_str) # Convert to integer result = num1_int + num2_int print(result)

What will be printed?

Practice Type Conversion

Now you try! Write code to ask the user for their birth year. Convert it to an integer using `int()`, calculate their approximate age in 2024, and print the age. Use the simulator below.
Focus: Interaction & Data Manipulation.

Output will appear here...

Input Mini Missions!

Combine `input()` and type conversion to solve these tasks. Enter sample input before testing each mission!

Mission 1: Simple Greeting

Ask the user for their name and print a greeting like "Hello, [Name]!". (No type conversion needed here!)

Mission 2: Age Next Year

Ask the user for their current age (as a number). Convert it to an integer using `int()` and print their age next year.

Mission 3: Price + Tax

Ask for an item price (can have decimals). Convert it to a float using `float()`. Calculate a 10% tax (price * 0.10) and print *only* the tax amount.

Module 4 Practice Complete!

Excellent work! You've practiced getting user input and handling different data types.
Ready for a bigger challenge? Try the Advanced Practice for Module 4!