3 Values
3.1 The Wall
You asked AI to build a temperature converter. It produced code that multiplied a string by a number and crashed with TypeError: can't multiply sequence by non-int of type 'float'. You had no idea what a “sequence” was or why multiplying was a problem. The formula looked right.
The issue was data types. AI treated the input as text when it needed to be a number. If you do not know the difference between "72" and 72, you cannot spot this in AI-generated code, and AI generates this bug constantly.
This chapter fixes that.
3.2 Thinking Session
3.2.1 Getting Oriented
In Python, what is the difference between the number 42 and the text “42”? Why does Python treat them differently, and what goes wrong when you mix them up? Give me examples of errors that happen when types get confused.
Your AI should explain that 42 is an integer and "42" is a string, and that Python does not automatically convert between them. If your AI says “Python is dynamically typed so it handles this for you,” push back, dynamic typing means variables can change type, not that types do not matter.
3.2.2 Go Deeper
What are the main data types in Python? I do not need all of them. Just the ones I will see most often in AI-generated code. For each one, show me what it looks like and one thing that catches people off guard about it.
Your AI should cover: int (integers), float (decimals), str (text), bool (True/False), and None. The gotchas matter: float division can produce unexpected precision (0.1 + 0.2 is not 0.3), booleans are actually integers (True + True is 2), and None is not the same as 0 or "".
How do I convert between types in Python? If AI gives me a string from user input and I need a number, how do I convert it? What happens if the conversion fails?
3.2.3 Challenge It
What will each of these produce, and why?
"5" + "3"
5 + 3
"5" + 3
int("5") + 3
float("hello")
"5" + "3" gives "53" (string concatenation). 5 + 3 gives 8. "5" + 3 crashes with TypeError. int("5") + 3 gives 8. float("hello") crashes with ValueError. If you can explain all five, you understand values.
3.2.4 What You Should Have Learned
- Python has distinct types: int, float, str, bool, None
"42"and42are fundamentally different- Type conversion is explicit:
int(),float(),str() - Conversion can fail:
int("hello")crashes - The
type()function tells you what you are working with
3.3 The Gap
When AI generates code that processes user input, it almost always needs type conversion. The input comes in as a string, but the code needs a number. Now you can spot that moment in the code and verify AI handled it correctly.
In the Building Session, you will make your chatbot respond differently to numbers versus text. The first step toward intelligent responses.
3.4 Building Session
3.4.1 The Spec
Make your chatbot recognise different types of input:
- If the user types a number, respond with a fun fact about it (even/odd, big/small)
- If the user types text, echo it as before
- Show the type of the input in debug-style output
3.4.2 Prompt It
Here is my chatbot so far:
"""PyBot v0.2: Terminal chatbot with structure."""
BOT_NAME = "PyBot"
VERSION = "0.2"
print(f"Hello! I'm {BOT_NAME} v{VERSION}.")
print("Type 'quit' to exit.")
while True:
user_input = input("You: ")
if user_input.lower() == "quit":
print(f"{BOT_NAME}: Goodbye!")
break
print(f"{BOT_NAME}: You said '{user_input}'")Update to v0.3. Add logic that: - Tries to convert the input to a number using int() - If it succeeds, responds with whether the number is even or odd - If it fails, treats the input as text and echoes it - Prints the type of the input in parentheses after the response - Use a try/except for the conversion (just the basic pattern)
Keep it simple. No functions yet, keep the while loop structure.
3.4.3 Read the Code
Your AI will produce something like this:
"""PyBot v0.3: Recognises numbers vs text."""
BOT_NAME = "PyBot"
VERSION = "0.3"
print(f"Hello! I'm {BOT_NAME} v{VERSION}.")
print("Type 'quit' to exit.")
while True:
user_input = input("You: ")
if user_input.lower() == "quit":
print(f"{BOT_NAME}: Goodbye!")
break
# Try to treat input as a number
try:
number = int(user_input)
if number % 2 == 0:
response = f"{number} is even"
else:
response = f"{number} is odd"
input_type = type(number).__name__
except ValueError:
response = f"You said '{user_input}'"
input_type = type(user_input).__name__
print(f"{BOT_NAME}: {response} ({input_type})")Line 16 uses int(user_input), explicit type conversion. If it fails, the except ValueError catches it and the code falls through to the string handling. type(number).__name__ gives the type as a readable string. The % operator checks even/odd. You will learn more about operators in Chapter 7.
3.4.4 Stretch It
Also handle float input. If the user types “3.14”, recognise it as a decimal number and respond with “3.14 rounded down is 3”. Try float() first, then int(), then fall back to string.
3.5 Your Chatbot So Far
- Ch 1: Basic loop with greeting and quit
- Ch 2: Docstring, constants, comments
- Ch 3: Recognises numbers vs text, shows input type
3.6 Quick Reference
# Types
x = 42 # int
x = 3.14 # float
x = "hello" # str
x = True # bool
x = None # NoneType
# Check type
type(x) # <class 'int'>
type(x).__name__ # 'int'
isinstance(x, int) # True
# Convert
int("42") # 42
float("3.14") # 3.14
str(42) # "42"
bool(0) # False
bool("") # False