Python from zero to data engineering Lesson 3 / 110

Text and input: let the audience answer back

Read user input, convert text to numbers, build readable messages with f-strings, and learn why '3' is not 3.

Changing the source code every time someone buys a ticket is an unusual business model. Today we will let the customer supply information while the program is running.

You need: variables, arithmetic, and print(). By the end: you can read text, convert a whole-number answer, and build a personalised message. Allow 25–35 minutes.

Ask, pause, remember

Create greeting.py:

customer_name = input("What is your name? ")
print("Welcome,", customer_name)

Run it, click the Shell window, type Mina, and press Enter. You should see:

What is your name? Mina
Welcome, Mina

input() displays its argument as a prompt, waits for a line of keyboard input, and returns that line as a string. Here, = assigns the returned string to customer_name. The space before the closing quote separates the prompt from the answer.

Returning and printing are different. input() returns a value that we can store. print() displays values. In print("Welcome,", customer_name), the comma separates two arguments; print() inserts a space between them by default.

The Enter key submits the answer; the trailing newline is not part of the returned string. Ordinary leading and trailing spaces you type are part of it. We will handle those shortly.

A number wearing a text costume

Predict this result:

ticket_text = input("How many tickets? ")
print(ticket_text * 2)

Enter 3. The output is 33, not 6. input() returned the string "3". Multiplying a string by an integer repeats the string. The same operator can have a different meaning for different types.

To do arithmetic, convert the input explicitly:

ticket_text = input("How many tickets? ")
ticket_count = int(ticket_text)
print(ticket_count * 2)

Now 3 produces 6. int() converts suitable text into a whole-number value. Keeping input and conversion on separate lines makes each step easier to inspect. Later, ticket_count = int(input("How many tickets? ")) is fine when you can explain that Python evaluates the inner call first.

Try three, then 3.5, on separate runs. Both cause a ValueError: the string is not a valid integer representation. int("3.5") does not round down. That is different from converting an already numeric float with int(3.5).

For this lesson, enter a valid whole number when prompted. We have discovered a real limitation; the program does not yet recover from malformed input. Exception handling will come later, after we can explain normal execution.

Put values inside a sentence

Run this complete example:

customer_name = "Mina"
ticket_count = 3
message = f"{customer_name} booked {ticket_count} tickets."
print(message)

Expected output:

Mina booked 3 tickets.

The f before the quote makes an f-string. Python evaluates expressions inside {} and includes their formatted values in the text. Without the f, the braces and names would be ordinary text.

We prefer this to "Mina booked " + ticket_count + " tickets.", which raises a TypeError: + cannot concatenate a string and an integer. You could explicitly convert the number with str(ticket_count), but an f-string is easier to read here.

F-strings are evaluated when that line runs. Changing ticket_count later does not update an already-created message, for the same reason that lesson two’s total did not recalculate itself.

Clean input deliberately

A string has operations called methods. A method is a function accessed through a value, using a dot. For example:

raw_name = "  Mina  "
clean_name = raw_name.strip()
print(f"Hello, {clean_name}!")
print(f"Original: [{raw_name}]")

Output:

Hello, Mina!
Original: [  Mina  ]

.strip() returns a new string without surrounding whitespace; it does not remove spaces between words. Strings are immutable: their contents cannot be modified in place. We store the returned value so we can use it.

Do not automatically lowercase or capitalise a person’s name. People get to choose how their names are written. For a machine command such as YES, however, normalising case can be helpful:

answer = "  YES  "
normalised_answer = answer.strip().lower()
print(normalised_answer)

This prints yes. The result of .strip() becomes the value on which .lower() is called. We will use this when making decisions in the next lesson.

Build a small receipt

Create receipt.py:

customer_name = input("Name: ").strip()
ticket_text = input("Number of tickets (whole number): ")
ticket_count = int(ticket_text)

ticket_price_cents = 850
total_cents = ticket_count * ticket_price_cents
euros = total_cents // 100
cents = total_cents % 100

print(f"Booking for {customer_name}")
print(f"Tickets: {ticket_count}")
print(f"Total: EUR {euros}.{cents:02d}")

With Mina and 3, the final three lines are:

Booking for Mina
Tickets: 3
Total: EUR 25.50

// 100 extracts whole euros from our positive cent amount; % 100 gives the remaining cents. Inside the f-string, :02d formats an integer with at least two digits, padding with zero on the left. Five cents becomes 05, not 5. Formatting changes the displayed text, not the stored value.

Why keep all that arithmetic in named variables? It exposes the steps. A one-line receipt might be shorter, but it would be harder to debug while you are learning.

Your turn: an awkward ticket price

Change the ticket price to 805 cents. Predict the receipt for one ticket and for three tickets, then run both. Also remove the f from the final print() argument and explain what happens.

Show the answers

One ticket displays EUR 8.05; three display EUR 24.15. Without f, Python prints the literal text Total: EUR {euros}.{cents:02d}. It no longer treats braces as replacement fields.

Checkpoint: input() always returns a string. Converting that string to an integer is a separate operation that can fail. Our receipt also accepts zero and negative counts, which should not be valid bookings. Next, we will make the program enforce those rules.

Search