Python for Profit

Building a Complete Inventory System

03

Making Your Program Interactive: User Input

The Big Idea

This chapter makes our program dynamic by learning how to ask the user for information using the input() function and then converting that information into the correct data types for use in our application.

Roadmap

  • The Problem with "Hard-Coded" Data: We'll discuss why writing data directly into the script is inflexible and how user input solves this.

  • The input() Function: Learn to prompt the user for information and store their response in a variable.

  • The Golden Rule of Input: Understand that input() always gives you back a string, even if the user types a number.

  • Type Conversion: The Right Tool for the Job: We'll learn how to convert the user's string input into the data types our program needs using int() and float().

  • Localization with a Currency Variable: As you suggested, we'll add a variable for the currency symbol, making our app's output easy to change for different regions.

  • Evolving main.py: We'll completely transform our script from a static report into a fully interactive one.

Full Chapter Content

Breaking Free from "Hard-Coded" Data

In Chapter 2, our main.py script worked perfectly, but it had a major limitation: the data was hard-coded. This means the product name, quantity, and price were written directly into the program's source code. To report on a different product, we had to manually edit the main.py file.

A professional application needs to be flexible. It shouldn't require a programmer to change its code for everyday use. It needs to work with data provided by the user. Today, we give our program the ability to listen.

Listening to the User with input()

Python has a built-in function for this exact purpose: input(). This function does two things:

  1. It displays a message (a "prompt") on the screen, asking the user for information.

  2. It pauses the program and waits for the user to type something and press Enter.

  3. It takes whatever the user typed and returns it so we can store it in a variable.

Let's see it in action. Open main.py and let's start fresh by asking for the user's name.

# --- PyInventory: A Step-by-Step Journey to Profit ---
# Chapter 3: Making Your Program Interactive

# Ask the user for their name and store it in a variable
user_name = input("Welcome to PyInventory! Please enter your name: ")

# Greet the user personally
print(f"Hello, {user_name}! Let's add a new product to the inventory.")

Save this and run it from your terminal: python main.py. You'll see the prompt, and the program will wait for you. Type your name and press Enter.

Welcome to PyInventory! Please enter your name: Alex
Hello, Alex! Let's add a new product to the inventory.

Our program is now interactive!

The Golden Rule of Input: Everything is a String

Let's try to get product details. We'll ask for the quantity. Add this line to your main.py:

product_quantity = input("Enter the product quantity: ")

If you run this and enter 50, you might think the product_quantity variable holds the number 50. But it doesn't.

The Golden Rule: The input() function always returns the data as a string.

So, when you type 50, Python stores "50" (the text characters '5' and '0') in the variable, not the integer 50. This causes a problem if you try to do math with it. If you were to add product_quantity * 2, you would get "5050", not 100!

Type Conversion: str to int and float

To solve this, we must explicitly convert the user's string input into the correct numeric data type. Python gives us simple functions for this:

  • int(): Takes a string (or float) and converts it to an integer.

  • float(): Takes a string (or integer) and converts it to a float.

Here is the correct way to get a number from the user:

# 1. Get the input as a string (as always).
quantity_str = input("Enter the product quantity: ")

# 2. Convert the string to an integer.
product_quantity = int(quantity_str)

# Now you can safely do math with it!
# print(product_quantity * 2) # This would correctly print 100

We can do the same for the price, using float() for the decimal value.

Updating main.py for a Fully Interactive Report

Now, let's combine everything we've learned to make our script fully interactive. We will also add a currency variable at the top. This is great practice—placing configuration settings like this at the beginning of a file makes them easy to find and change later.

Replace the entire contents of your main.py with this final version for Chapter 3:

# --- PyInventory: A Step-by-Step Journey to Profit ---
# Chapter 3: Fully Interactive Inventory Entry

# --- Configuration ---
# By placing the currency symbol in a variable at the top,
# we can easily change it later for different regions.
currency = "MAD" 

# --- User Welcome ---
print("--- Welcome to PyInventory ---")
print("Let's add a new product to your inventory.")
print("---------------------------------------")

# --- Data Input ---
# Get product details from the user.
product_name = input("Enter the product name: ")
quantity_str = input(f"Enter the quantity for {product_name}: ")
price_str = input(f"Enter the price per item (in {currency}): ")

# --- Data Conversion ---
# Convert the user's text input into numbers.
# We will learn about error handling in a later chapter.
quantity = int(quantity_str)
price = float(price_str)

# --- Calculation ---
# Calculate the total value of the new inventory.
total_value = quantity * price

# --- Report Output ---
# Display a clean, formatted report to the user.
print("\n--- New Inventory Item Added ---")
print(f"Product: {product_name}")
print(f"Quantity: {quantity}")
print(f"Price: {currency}{price}")
print(f"Total Stock Value: {currency}{total_value}")
print("--------------------------------")

Save and run the file. The program will now guide you through entering a new product, and the final report will be perfectly formatted with the currency symbol you defined.

Chapter 3: Summary & What's Next

This was a huge step. Our program is no longer static; it's a dynamic tool that responds to the user.

  • You learned to get user data with the input() function.

  • You mastered the golden rule: input() always returns a string.

  • You used int() and float() to convert strings to the correct numeric types for calculations.

  • You made the code more professional by using a currency variable for easy localization.

The problem now is that our program has the memory of a goldfish. As soon as it finishes running, all the data we entered is gone. In the next chapter, we'll learn to handle not just one item, but a whole collection of them using Python's most versatile data structure: the List.