Search code examples
pythonfunctionif-statementsyntaxindentation

How would I put an if statement inside a function?


I am very very new to Python and before this I only used extremely simple "programming" languages with labels and gotos. I am trying to make this code work in Sikuli:

https://i.sstatic.net/aXVPv.png

Basically I want it to loop the if statement until any of the images is found, and if one of them is found, it executes the right function and starts looping again until it detects a new command.

I have been looking for tutorials and documentation, but I'm really lost. I think my mind is too busy trying to go from a goto/label to an organized programming language.

If someone could give me an example, I would appreciate it a lot!


Solution

  • In Python indentation matters, your code should look like this:

    def function():
        if condition1:
            reaction1() 
        elif condition2:
            reaction2()
        else:
            deafult_reaction()
    

    I recommend reading the chapter about indentation in Dive Into Python as well as PEP 0008.