CS 161 - Module 2: Input & Casting
1. Introduction – How to Get Users to Talk to Your Code
Writing code is fun, but at some point, you’ll want your program to interact with real humans (or at least let them enter data). That’s where the input() function comes in. But beware—all input comes in as a string, even if the user types a number. That’s why we sometimes need to convert (cast) input into the right type.
Key Takeaways:
input() lets users enter data.
All input is received as a string.
Use int(), float(), and str() to convert (cast) input into the correct type.
Forget to cast? Expect weird errors!
💡 Fun Fact: If you ask Python to convert "hello" into an integer, it will panic and crash—just like us when asked to do math unexpectedly.
2. Using the input() Function
The input() function lets the user type something into the program. You can use it in two ways:
# Method 1: Basic input
name = input()
print("Hello", name)
# Method 2: Input with a prompt
name = input("Please enter your name: ")
print("Hello", name)
Difference Between These Two?
The first version asks for input, but the user doesn’t see a prompt.
The second version prints a message before taking input, making it more user-friendly.
💡 Best Practice: Always use a prompt inside input()—it makes your code easier to use.
3. Casting – Converting Input to Numbers
Since input() always returns a string, we need to convert input into numbers when doing calculations.
Example: Asking for a Number
age = int(input("Enter your age: ")) # Converts input into an integer
print("You are", age, "years old.")
height = float(input("Enter your height in meters: ")) # Converts input into a float
print("Your height is", height, "meters.")
💡 Warning: If the user enters text instead of a number, Python will crash with an error.
4. Step-by-Step Casting
You can also store the input first and convert it separately.
str_age = input("Enter your age: ") # Stores input as a string
int_age = int(str_age) # Converts it to an integer
print("In 5 years, you will be", int_age + 5)
💡 Use this when debugging! It helps separate where things might go wrong.
5. Handling Invalid Input
What happens if the user enters something that can't be converted (e.g., typing "hi" when asked for a number)?
value = int(input("Enter a number: ")) # If the user enters "hello", Python will crash!
💡 Best Practice: Always assume users will try to break your code. We'll learn how to handle errors later!
6. More Type Casting – Changing Between Types
Converting an Integer to a Float
print(float(18)) # Output: 18.0
Converting a Float to an Integer (Removes Decimals)
print(int(3.99)) # Output: 3 (truncates, doesn't round)
Converting Numbers to Strings
print(str(-9.4)) # Output: "-9.4"
print(str(100)) # Output: "100"
💡 Takeaway: Python is flexible, but it won’t guess conversions for you—you need to tell it what to do.
7. Interactive Learning & Challenges
Try running these examples in Python Tutor (pythontutor.com) to see how they work step by step.
Exercises
1. Ask for a name and greet the user
name = input("Enter your name: ")
print("Hello", name)
✅ Expected Output (if user enters Taylor Swift): Hello Taylor Swift
2. Ask for two numbers and multiply them
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("The result is", num1 * num2)
✅ Expected Output (if user enters 3 and 20): The result is 60
3. Store the multiplication result in a variable before printing
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = num1 * num2
print("The result is", result)
✅ Expected Output (if user enters 3 and 20): The result is 60
💡 Challenge Yourself! Try asking the user for a decimal number and calculating its square.
8. Reflection: What We Learned
input() collects user input as a string.
Casting converts input into numbers when needed.
Converting floats to ints removes decimals (doesn’t round).
Always assume users will enter the wrong thing—Python doesn’t forgive mistakes.
9. Conclusion & Call to Action
Now that you know how to accept input and cast data types, try making a simple calculator!
💡 Question for You: Have you ever written code that crashed because of bad input? Share your funniest errors! 🚀