Chapter 4: User Inputยถ

๐Ÿš€ Open Notebookยถ

Open In Colab Open In Kaggle

๐Ÿ“บ Video Tutorialยถ

Watch on YouTube

User input in Python is easy + exercises โŒจ๏ธ (9:48)

๐Ÿ“š What Youโ€™ll Learnยถ

Learn how to make your programs interactive by accepting input from users!

๐ŸŽฏ Learning Objectivesยถ

  • Use the input() function to get user data

  • Understand that input() always returns a string

  • Combine input() with type casting for numeric inputs

  • Create interactive programs that respond to user input

๐Ÿ“– Concept Explanationยถ

The input() Functionยถ

The input() function is used to get information from the user. It:

  1. Displays a message (prompt) to the user

  2. Waits for the user to type something

  3. Returns what the user typed as a string

Syntaxยถ

variable = input("Your prompt message: ")

Important: input() Always Returns a String!ยถ

Even if the user types a number, input() returns it as text:

age = input("Enter your age: ")  # If user types 25, age = "25" (string)
# To do math, convert it:
age = int(input("Enter your age: "))  # Now age = 25 (integer)

๐Ÿ’ก Examplesยถ

Example 1: Simple Text Inputยถ

name = input("What is your name? ")
print(f"Hello, {name}!")

Example 2: Numeric Inputยถ

# Wrong way (can't do math with strings):
age = input("Enter your age: ")
# next_year = age + 1  # Error!

# Correct way:
age = int(input("Enter your age: "))
next_year = age + 1
print(f"Next year you'll be {next_year}")

Example 3: Multiple Inputsยถ

first_name = input("First name: ")
last_name = input("Last name: ")
age = int(input("Age: "))

print(f"Hello {first_name} {last_name}, you are {age} years old!")

Example 4: Float Inputยถ

height = float(input("Enter your height in meters: "))
print(f"Your height is {height}m")

โœ๏ธ Practice Exercisesยถ

  1. Create a program that asks for the userโ€™s name and favorite color, then prints them

  2. Make a program that asks for two numbers and prints their sum

  3. Ask for a userโ€™s birth year and calculate their age (assume current year is 2025)

  4. Create a mad libs style program that asks for several words and creates a story

  5. Ask for temperature in Celsius and convert it to Fahrenheit

๐Ÿ” Common Mistakesยถ

Mistake 1: Forgetting Type Conversionยถ

# Wrong:
age = input("Age: ")  # age is "25" (string)
print(age + 1)  # Error! Can't add string and number

# Correct:
age = int(input("Age: "))  # age is 25 (integer)
print(age + 1)  # Works! Prints 26

Mistake 2: Wrong Conversion Typeยถ

# If user enters "3.14":
number = int(input("Enter number: "))  # Error! Can't convert "3.14" to int

# Use float() instead:
number = float(input("Enter number: "))  # Works! number = 3.14

Mistake 3: Forgetting the Promptยถ

name = input()  # Works but user doesn't know what to enter
name = input("Enter your name: ")  # Better! Clear instruction

๐Ÿ“ Input Patternsยถ

Pattern 1: Input on Same Line as Conversionยถ

age = int(input("Enter age: "))

Pattern 2: Input Then Convert (easier to debug)ยถ

age_str = input("Enter age: ")
age = int(age_str)

Pattern 3: Input with Validation (advanced)ยถ

age = input("Enter age: ")
if age.isdigit():  # Check if input is a number
    age = int(age)
    print(f"You are {age} years old")
else:
    print("Please enter a valid number")

๐ŸŽฎ Interactive Program Exampleยถ

# Simple greeting program
name = input("What's your name? ")
age = int(input("How old are you? "))
city = input("Where do you live? ")

print(f"\nNice to meet you, {name}!")
print(f"You are {age} years old and live in {city}.")
print("Welcome to Python programming!")

๐Ÿš€ Try It Yourselfยถ

Create an interactive program that:

  1. Asks the user for their name

  2. Asks for their favorite number (integer)

  3. Asks for their favorite food

  4. Creates a personalized message using all the information

๐ŸŽ“ Key Takeaways from Videoยถ

  1. Functions are reusable blocks of code

  2. Use if-elif-else for conditional logic

  3. Follow along with the video for hands-on practice

๐Ÿ’ก These points cover the main concepts from the video tutorial to help reinforce your learning.

๐Ÿ”— Next Chapterยถ

Continue to Chapter 5: Madlibs Game to create a fun word game using user input!