Skip to Content
Course content

ODOO PYTHON BOOTCAMP / LECTURE 1.3

Control flow, with a Monopoly turn.

Learn how Python chooses what happens next, using a small example you can follow one step at a time.

PYTHON FUNDAMENTALS  ·  READ · PREDICT · RUN  ·  BUILDS ON LECTURE 1.2

01 · OUR EXAMPLE

A turn has a starting point

Imagine one player is on square 7 and rolls a total of 5. The player moves forward five squares, landing on square 12. Our program needs to answer ordinary questions: What square do they land on? Is that square a property? If it is, is it for sale? The answers determine what Python does next.

For this lesson, keep the game small. A position is the number of the square where the player is standing. A roll is the number of squares to move. We are using these as simple Python numbers; we are not building the whole game yet.
CONTROL FLOWFollow the steps in order
Start at square 7
+
Roll 5
→
Land on square 12
→
Check what is there
First we calculate the new position. Then we make a decision about that square.
position = 7
roll = 5
new_position = position + roll

print(new_position)  # 12

Python runs these lines from top to bottom. It stores 7 in position, stores 5 in roll, adds them, and stores the answer in new_position. The program has calculated a result, but it has not made any choices yet.

02 · MAKE A DECISION

Use if when something should happen only sometimes

Suppose square 12 is a property. If it is unowned, we want to offer it to the player. If someone already owns it, we do something else. An if statement lets Python check a condition and choose a path.

is_property = True
is_owned = False

if is_property and not is_owned:
    print("You can buy this property.")
else:
    print("This property is not available to buy.")

Read the condition out loud: “Is this a property, and is it not owned?” Both parts must be true for the first message to print. The word and combines two checks. The word not reverses a true/false value.

CONTROL FLOWFollow the steps in order
Check: property and unowned?
→
Yes: offer it
or
No: explain it is unavailable
Only one of these two blocks runs for this turn.

A condition is a question Python can answer with True or False. For example, cash >= price asks whether the player has enough money. The symbols >= mean “greater than or equal to.”

cash = 240
price = 200

if cash >= price:
    print("The player has enough cash.")
else:
    print("The player needs more cash.")

Try changing cash to 150. The code stays the same, but the answer changes. That is the useful part: a rule can handle many different turns because it checks the current values each time.

03 · MORE THAN TWO OPTIONS

Use elif when there are several cases

A square can have more than two possible states. For our small example, say it is either an unowned property, a property someone owns, or not a property at all. We can check those cases in order.

is_property = True
is_owned = True

if not is_property:
    print("Follow the rule for this kind of square.")
elif not is_owned:
    print("Offer the property for sale.")
else:
    print("The property already has an owner.")

elif means “otherwise, check this next condition.” Python stops at the first condition that is true. Here, the first check is false, the second is also false, so the else block runs.

Order matters. Put the specific cases where they can be checked clearly. If an earlier condition already matches, Python will not look at later branches.

You will see this pattern in business software too. An order may be new, confirmed, shipped, or cancelled. A support request may be open, waiting, or resolved. The labels differ, but the program is doing the same job: check the current state and choose the appropriate action.

04 · REPEAT WORK

Use a for loop to visit each item

Now imagine we have a list of players and want to display each player’s name. We could write a separate print line for every player, but that gets tedious as the list grows. A for loop repeats the same instructions for each item.

players = ["Maya", "Luis", "Sam"]

for player in players:
    print("Ready for your turn,", player)

Python takes the first name, runs the indented line, then takes the next name and runs that line again. After Sam, there are no more names, so the loop ends. The variable player refers to the current name during each pass.

CONTROL FLOWFollow the steps in order
Maya
→
run the indented code
→
Luis
→
run it again
→
Sam
The loop ends after it has handled every name.

We can also make a small report from a list of property prices. This is a familiar task in an Odoo system: visit a set of records and calculate or display something for each one.

prices = [200, 150, 300]

for price in prices:
    print("Property price:", price)

Later, when you work with Odoo records, the same idea helps you understand code that processes several orders, customers, or products. The recordset is different from this simple list, but the basic question is familiar: “For each item, what should I do?”

05 · REPEAT UNTIL A CONDITION CHANGES

Use while when you do not know the number of repeats

A for loop is a good fit when you have a collection and want to visit every item. A while loop is useful when you want to keep going until something changes. For example, let’s move a token around a simplified track until it reaches square 10.

position = 7

while position < 10:
    position = position + 1
    print("Now on square", position)

Python checks position < 10 before each pass. It prints 8, then 9, then 10. When the position is 10, the condition is false, so the loop stops.

A while loop must be able to stop. Something inside the loop should eventually make its condition false. If the position never changed here, Python would keep printing forever. That is called an infinite loop.

Use for when you can say “once for each item.” Use while when you can say “keep doing this while the condition remains true.” Those two questions are usually enough to choose.

06 · FOLLOW THE VALUES

Trace a program instead of guessing

When code feels confusing, write down the values as Python runs each line. Here is a short example that totals a player’s cash after collecting two payments.

cash = 100
payments = [20, 15]

for payment in payments:
    cash = cash + payment

print(cash)
PassPaymentCash beforeCash after
Start——100
120100120
215120135

The output is 135. Notice that cash changes during each pass, while payment holds the current item from the list. A little table like this is a practical debugging tool: it shows exactly where your expectation and the program’s behavior begin to differ.

07 · A RISK-STYLE EXAMPLE

Check each territory against a simple rule

Here is a second setting from the course. In this simplified Risk-style exercise, each territory already tells us two things: who owns it, and whether it is next to the player's territory. We will look for neighboring territories owned by someone else. This is a practice rule for learning Python; it is not the full set of Risk rules.

What the program is given

The list contains territory names, owners, and an is_adjacent yes-or-no value. We are not calculating the map or deciding what a player is allowed to do in the full game. We are practicing a loop and two checks.

RISK-STYLE TERRITORY CHECKVisit each territory, then apply the same rule
Territory list
→
Check: adjacent and owned by someone else?
→
Print whether it matches
The loop handles one territory at a time. The condition decides what message to print for that territory.
territories = [
    {"name": "Northland", "owner": "Maya", "is_adjacent": True},
    {"name": "East Cape", "owner": "Luis", "is_adjacent": True},
    {"name": "South Ridge", "owner": "Luis", "is_adjacent": False},
]
player = "Maya"

for territory in territories:
    if territory["is_adjacent"] and territory["owner"] != player:
        print(territory["name"], "matches our practice rule")
    else:
        print(territory["name"], "does not match our practice rule")

The program prints that East Cape matches: it is adjacent, and Luis owns it rather than Maya. Northland is adjacent but Maya already owns it. South Ridge is owned by Luis, but it is not adjacent. Each territory must pass both checks to match the rule.

Notice how the loop and the condition have different jobs. The for loop says, “visit every territory.” The if says, “for this territory, check these two facts.” Keeping those jobs separate makes the code easier to explain and change.

08 · AN AIR-TRAFFIC-CONTROL EXAMPLE

Display a status without pretending to make the decision

Air traffic control is a serious, safety-critical setting, so our small Python example will have a very limited job: display a status that has already been entered by a controller or supplied by another trusted part of a system. This code does not decide whether an aircraft can move, and it is not suitable for real air-traffic operations.

Each flight in this example has a callsign and a clearance status. We will show one message for a cleared status, another for waiting, and a verification message for anything else. Handling the unexpected case matters: real data can be missing, mistyped, or use a value our program did not expect.

STATUS DISPLAY ONLYThe program reports a supplied status
Status entered elsewhere
→
Python checks the status
→
Display a clear message
The program reports information. A qualified human and the approved operational systems remain responsible for real decisions.
flights = [
    {"callsign": "NORTH 21", "status": "cleared"},
    {"callsign": "EAST 08", "status": "waiting"},
    {"callsign": "WEST 14", "status": "unknown"},
]

for flight in flights:
    if flight["status"] == "cleared":
        print(flight["callsign"], "— clearance received")
    elif flight["status"] == "waiting":
        print(flight["callsign"], "— waiting for an update")
    else:
        print(flight["callsign"], "— ask the controller to verify")

The for loop visits all three flights. The if and elif handle the two statuses we recognize. The final else catches the unfamiliar value "unknown" and asks for verification. It is safer and clearer to handle an unexpected value than to silently treat it as “cleared.”

This example shows a durable engineering habit: be clear about what your program is responsible for, and what it is not. In business systems, a display or report may show a decision made elsewhere; it should not quietly take over authority it was never designed or approved to have.

09 · CONNECTING TO ODOO

These same decisions appear in business software

Odoo methods often check a record’s values, choose a result, or process several records. The shape of the code will be familiar even when the business meaning is new.

for order in orders:
    if order.amount_total > 1000:
        print("Review this large order:", order.name)
    else:
        print("Normal order:", order.name)

This is plain Python illustrating the idea; it is not a complete Odoo method. orders stands for a group of orders, and each order has a total and a name. In later lessons, we will learn where Odoo records come from and how to write methods that follow Odoo’s conventions.

These examples also point to durable skills we will keep practicing: understand the business process before automating it, make each rule explicit, consider what the user is allowed to see or change, and test ordinary as well as unusual cases. AI can help write code, but it cannot safely guess what a business rule should mean. A person still needs to explain the process, check the design, and verify the result.

THE SAME PYTHON TOOLS, DIFFERENT SETTINGSDecisions and loops show up across the course
Monopoly: follow a game turn
→
Risk: check territory information
→
Air traffic: display a supplied status
→
Odoo: process business records
The Risk and air-traffic examples in this lesson are deliberately small teaching examples. They are not complete game rules or real operational systems.

10 · TRY IT YOURSELF

Predict, run, and explain

  1. Set position = 7 and roll = 5. What is new_position?
  2. In the property example, change is_owned to True. Which message prints, and why?
  3. In the Risk-style example, change East Cape's is_adjacent value to False. What message does it print now?
  4. In the status-display example, change West 14's status to "waiting". Which branch handles it?
  5. Add 25 to the payments list. What is the new total?
  6. In the while example, change the starting position to 9. How many times does the loop run?
  7. What could go wrong if a while loop never changes the value used by its condition?

Before running each example, say what you expect. Then run it and compare. If the output differs, trace the values line by line. That habit will serve you throughout Python and Odoo development.

11 · QUICK RECAP

What you can do now

  • Use if, elif, and else to choose between actions.
  • Use a for loop to handle each item in a collection.
  • Use a while loop to repeat while a condition remains true.
  • Trace changing values to understand what a program does.
  • Explain the inputs and practice rule in a game example.
  • Recognize these patterns in simple Odoo business logic.

Next, we will learn how to put related instructions into functions. That will let us give a name to a small piece of work and reuse it, such as “move the player” or “check whether an order needs review.”

In this lecture, we will delve into the concept of control flow in Python, a fundamental aspect of programming that allows us to dictate the execution path of our code based on certain conditions. Understanding control flow is crucial for writing effective algorithms, making decisions in our applications, and manipulating data effectively, especially when working with Odoo.

Commenting is not enabled on this course.