AI Hints
Home›Python Tutorial›Numbers, Text and Decisions
Python · Lesson 6 of 49

Input and Decisions

Ask questions, and choose what to do.

⏱ 3 min read▶ Video at 17:35🎓 Free
Watch this lesson in the Python courseWatch on YouTube ↗Full Python course

Customers keep asking Ava the same question. Is my order eligible for free delivery? She wants a program that asks for the order total, and answers on its own. For that, the program needs two new skills. Asking the user for information, and making a decision.

Input and Decisions
From the video: Input and Decisions
  • Skill one: ask the user for input
  • Skill two: make decisions with if
  • Goal: a free delivery checker

Asking with input()

The input function pauses the program, shows a message, and waits for the user to type. Whatever they type comes back as a string, always. Even if they type a number, it is still text. To do maths, convert it, with int for whole numbers, or float for decimals.

ask.pypython
name = input("Your name: ")
print(f"Hello, {name}!")
qty = int(input("How many plants? "))
print("Ordered:", qty)
print("Next order:", qty + 1)
Input typed: Maya · 3
Output
Your name: Maya
Hello, Maya!
How many plants? 3
Ordered: 3
Next order: 4

Comparison operators

To decide, Python compares values. Double equals checks if two values are equal. Remember, a single equals stores a value, and a double equals asks a question. Exclamation equals means not equal. Then greater than, less than, and their or equal versions. Every comparison gives back a boolean, True or False.

OperatorMeaningExampleResult
==equal to5 == 5True
!=not equal5 != 3True
>greater than3 > 5False
<less than3 < 5True
>=at least5 >= 5True
<=at most4 <= 3False

if, elif and else

Now the decision. The if keyword checks a condition, and ends with a colon. The indented lines below run only when the condition is true. Use elif for more conditions, and else for everything left over. The indentation, four spaces, is how Python knows which lines belong together. It is not decoration, it is the syntax.

delivery.pypython
total = 65
if total >= 100:
    print("Free delivery + gift!")
elif total >= 50:
    print("Free delivery!")
else:
    print("Delivery costs $5")
Output
Free delivery!

and, or, not

Combine conditions with and, or, and not. And needs both sides to be true. Or needs at least one. Not flips true to false. Here, Ava gives a student discount only to students who also order at least two plants. Notice how the code reads almost like English.

logic.pypython
is_student = True
plants = 3
if is_student and plants >= 2:
    print("Student discount applied")
if plants > 10 or is_student:
    print("You get a free pot")
if not is_student:
    print("Join our student club")
Output
Student discount applied
You get a free pot

Truthy and falsy

Python can treat any value as true or false. Zero, an empty string, an empty list, and None count as false. Almost everything else counts as true. So you can write, if name, to check that the text is not empty. It is a small trick, loved by Python programmers.

False values0, 0.0, '', [], {}, None
True valueseverything else
Handy checkif name: means name is not empty
Prefer clear codeexplicit is fine too

The free delivery checker

Time to build Ava's checker. We ask for the total, convert it, and decide. Orders of fifty or more ship free, and others pay five dollars. Run it with a small order, and it tells the customer exactly how much more to spend. That is a real, useful program, in just a few lines.

checker.pypython
total = float(input("Order total: $"))
if total >= 50:
    print("Free delivery!")
else:
    need = 50 - total
    print(f"Add ${need:.2f} more for free delivery")
Input typed: 32.5
Output
Order total: $32.5
Add $17.50 more for free delivery

Plant care advisor

Your turn. Ask the user how many days since they watered their plant. If it is seven days or more, print, Time to water. Otherwise, print, It is fine for now. Ready? Here is one solution. Convert the input to an int, compare, and print. Try it with different numbers.

✍️ Exercise: Plant care advisor

Ask for days since watering. 7 or more: "Time to water". Otherwise: "It is fine for now".

Show hint

int(input(...)) then if / else

Show solution
water.pypython
days = int(input("Days since watering: "))
if days >= 7:
    print("Time to water")
else:
    print("It is fine for now")
Output
Days since watering: 9
Time to water

Summary

Brilliant. Your programs can now ask questions and make decisions. You know that input always returns text, how to convert it, how comparisons work, and how if, elif and else choose a path. Next, Ava wants the computer to repeat work for her, with loops.

Key takeaways
  • input() returns text: convert with int/float
  • Compare with == != > < >= <=
  • if / elif / else, with indentation
Next up: Loops — repeat work without repeating code