Module 4: Interacting with Your AI

Learn how Python gets input from users and handles different data types.

🗣️ Let's Talk!

Welcome to Module 4! So far, our code runs the same way every time. But what if we want our program (or AI) to be more dynamic? What if it needs information from the user while it's running?

This is crucial for setting parameters, providing data, or guiding an AI's behavior. Python has a built-in way to pause and ask the user for input. Let's see how!

⌨️ Getting Input with `input()`

Python uses the `input()` function to ask the user a question and wait for an answer.

The Prompt

Your program displays a message asking for information.

➡️
⌨️

User Responds

The user types their answer and presses Enter.

➡️
✉️

`input()` Captures Text

The `input()` function receives the user's typed response as text (a string).

`input()` provides the bridge for user interaction, always delivering the result as text.

⚠️ The Catch: Everything's Text!

There's one crucial detail about `input()` you MUST remember.

🧐

No matter what the user types – even if it looks like a number – the `input()`Always returns the user's typed characters as a string (text). function ALWAYS returns the data as a string (`str`)Remember? Text data enclosed in quotes..

This means if you try to do math directly with the result, you might get unexpected behavior (like text joining instead of adding)!

🔧 Conversion Tools: `int()`, `float()`, `str()`

Since `input()` gives us text, we often need to convert it to the *actual* type we need.

🔢

`int()`

Converts a string (or float) into a whole number (integer). Fails if the string isn't a valid whole number.

💧

`float()`

Converts a string (or integer) into a decimal number (float). Fails if the string isn't a valid number.

🔡

`str()`

Converts almost any other data type back into its text representation (string).

These functions are essential for handling user input correctly!

⚙️ Input & Conversion in Action

Let's ask the user for some AI parameters and use them.

year_prompt = "Target year for AI goal: " target_year_str = input(year_prompt) # Get input (always str) # We need an integer for calculations, so convert! target_year_int = int(target_year_str) # Convert str to int accuracy_prompt = "Desired accuracy (e.g., 0.95): " accuracy_str = input(accuracy_prompt) # Get input (always str) # Need a float for precision, convert! accuracy_float = float(accuracy_str) # Convert str to float print("Setting goal for year:", target_year_int) print("Aiming for accuracy:", accuracy_float) print("Input types were:", type(target_year_str), type(accuracy_str)) print("Converted types are:", type(target_year_int), type(accuracy_float))

This code asks the user for two pieces of information: a target year and a desired accuracy level.

Notice how we first use `input()`Gets the user's text input using the provided prompt message. to get the data, storing the results (which are strings) in `target_year_str` and `accuracy_str`.

Crucially, we then use `int()`Converts the year string into an integer number. and `float()`Converts the accuracy string into a floating-point (decimal) number. to convert those strings into the numerical types we actually need for potential calculations or comparisons.

Finally, we print the converted values and use `type()` to confirm the successful change from `str` to `int` and `float`. This is fundamental for handling user-provided data effectively!

🧠 Quick Check!

Module 4 Theory Complete!

Fantastic! You now know how to make your Python programs interactive using `input()` and how to handle the data correctly with type conversions.
Time to practice these essential skills! Head to the Practice Zone or tackle the Advanced Practice for Module 4.