Python!

Hi.
While i was checking some other threads i saw a few code snippets and they looked pretty nice and simple, but i got lost about afew things.
If you wish please help me bit with some simple code snippets.

When do i use (while) , know what i mean but i have no idea where to apply it.
What about (return) .
Thank you!:slight_smile:

The “while” statement is used to loop while some expression evaluates to True.

It’s not very common, because most loops are used to iterate over some sequence of elements, in order to modify them, and there, the “for” statement is more convenient.

If you see a while statement used in blender, it would probably be in situations like:


new_random = last_random
while last_random == new_random:
    new_random = random.randint(0, 3)

You don’t want to get the same random as the last, so you use while to try again, until you get something different.

As for the “return” statement: it’s used to return a value from a function. When you have a regular function like this:


def myFunc(arg):
    r = arg + 1
    print(r)
    return # optional

The returned value is a NoneType object, or “None”, as it’s referred to in python.

Here:


def myFunc(arg):
    r = arg + 1
    return r
    print(r)

The “r” object is returned, and print® is not executed.

I would recommend that you go through a python tutorial, where this is all covered (google “python tutorial” for relevant links).

I suggest to go through “Byte of Python” it explains all this basics step-by-step.

Thank you Goran,really useful and simple, i will try and make my code and use (return) and (while)
I will also check that, Monster.