Python Conditions

In daily life we always have to make decisions and most often we do not really think about them. Making a decision means that there is always at least one alternative we could have chosen instead. The entire process is based on certain factors or parameters we have to evaluate to make the final decision. With programming the process is just the same. You have to distinguish between different conditions, which will influence the way the script finally goes. For this purpose, programming languages recognise a construction called “if and else“. You know this from your own daily experience:

“If the weather is fine, I go out for a walk, if not I will stay at home and read a book.”

That is exactly the way “if-else” works. If a certain condition is fulfilled, the script follows a given route to achieve a desired result. If the condition is not satisfied, the program has to follow another path. A very nice example for this type of decision-making is planning a holiday trip. This is something where you really have to consider many things, because holidays depend on so many factors: your partner, money, destination, time, personal preferences, the hotel, airline and so on. The first question is whether you can afford a certain destination or not:

my_money      = 1500.00
accommodation = 930.00
flight        = 410.00
leisure       = 200.00

if (accommodation + flight + leisure <= my_money):
    destination = "Norway"

else:
    destination = "Ireland"

scene.message(destination)

 

In this example you have to find a total price. This can be done by adding up the individual entries with a “+“ operator. Then we have to compare the result against the available amount of money with a “<=” operator. If the total amount is smaller than the available amount of money, the destination will be Norway. Since Norway’s too expensive (930.00 + 410.00 + 200.00 = 1540.00), it will be Ireland. 

It is also possible to make a comparison like the follwoing:

my_money      = 1500.00
birthday_gift = 200.00
total_costs   = 1555.00

if (total_costs <= my_money + vacation_benefit) 

or vice versa:

if (my_money + birthday_gift >= total_costs)

 

With Boolean operators we can make decisions based on more than one factor. Boolean operators, as we already mentioned, being “and”, “or”, “not”. The little script below tests for two parameters. If both conditions are fulfilled the destination will be Sweden: 

total_money     = 1700.00
favourite_hotel = "Holiday Inn Stockholm"
total_costs     = 1540.00

if ((total_costs <= total_money) and (available_hotel == favourite_hotel)):
    destination = "Sweden"

else:
    destination = "Belgium"

 

The nice thing with “if” and “else” is that you can compare against variables, values and calculations. This property makes them very flexible and versatile, and you will soon find out that if and else constructions are very often needed. There is almost always a case where you have to differentiate and follow a new path. But sometimes it is not enough to define these simple “if-else” constructions and you need more options. Let's assume you want to choose a destination based on a certain month. You just typed in a certain number representing a month and the script chooses the appropriate country for you:

if (input == 4):
    destination = "Italy"

elif ((input >= 5) and (input < 8)):
    destination = "Hungary"

elif ((input >= 8) and (input <= 10)):
    destination = "Cyprus"

else:
    destination = "Oh no! I have to stay at home..."

scene.message(destination)

 

The “elif” statement means “else if” and can be used as often as required, while “if” and “else” can only be used once within a “if-else” construction. But, of course, you are able to place as many if-else conditions in your script as you want. Another issue is that it is mandatory to use an “if” condition, but “else” and “elif” are optional. This code snippet is a correct and complete instruction:

input = 3

if ((airline == "National Airways") and (flight_price >= 750.00)):
    available_destinations = ["Finland","France","Germany","Poland","Spain"]
    selection = available_destinations[input]

    scene.message(selection)

 

It is also allowed to build nested “if-else” constructions for even more differentiations. In such a case it is a good idea to make appropriate comments to keep the overview.

if (my_money >= total_costs):
  
    if (airline == "National Airways"):
        destination = "Slovenia"

    elif (airline == "Best Fly"):
        destination = "Austria"

    else:
        destination = "Switzerland"

else:
    destination = "I obviously have to stay at home!"

 

With “if-else” conditions it is possible to cover any situation and case, go to a certain routine or calculation, call a new function, or even exit a script. You can differentiate between various values and results, and make the desired decision to achieve a certain result.