Chapter 4: User Inputยถ
๐ Open Notebookยถ
๐บ Video Tutorialยถ
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 dataUnderstand that
input()always returns a stringCombine
input()with type casting for numeric inputsCreate interactive programs that respond to user input
๐ Concept Explanationยถ
The input() Functionยถ
The input() function is used to get information from the user. It:
Displays a message (prompt) to the user
Waits for the user to type something
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ยถ
Create a program that asks for the userโs name and favorite color, then prints them
Make a program that asks for two numbers and prints their sum
Ask for a userโs birth year and calculate their age (assume current year is 2025)
Create a mad libs style program that asks for several words and creates a story
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:
Asks the user for their name
Asks for their favorite number (integer)
Asks for their favorite food
Creates a personalized message using all the information
๐ Key Takeaways from Videoยถ
Functions are reusable blocks of code
Use if-elif-else for conditional logic
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!