Vizard 7 » Tutorials & Examples » Python Programming » If-then logic
7.7

If-then logic

Sometimes you only want to run lines of script if a condition is true. In an instance of that sort, you tell the program, “If condition 1 is true, then execute one set of commands. Otherwise, execute another set of commands.” Add the following lines to your script:

for number in range(5):
    if cows > 1:
        print("Join us for a barbeque")
        cows = cows -1
    elif cows == 1:
        print("Last barbeque today")
        cows = cows -1
    else:
        print("No barbeque today")

Now run the program. With these lines, we have embedded an if statement inside of a for loop. So, the program looped through the if statement 5 times. Each time lines were only executed if they met one of the if conditions. Notice that we used elif to add a condition. elif is just the Python way of saying “or else if”. When an instance does not fit any of the conditions the script runs the statements under else.

Note: make sure you use two equal signs together when you're testing for an equal condition (“==”). That way Vizard knows that you're comparing the value of a variable and not setting that variable equal to something else.

Speaking Vizard's language

A quick note: Tips on scripts

Looping logic

If-then logic

Functions

An example