Looping statements

Can anyone point me in a direction of material that will help me, I have built a simple layout with a robot and conveyors as shown below, have very basic code, I an having trouble looping the code as i generally have no idea how to so if anyone can help with direction to go in would be much help

Hi,

I think the simplest way here is to use while True loop like this:

def OnRun():
    # init warmup values if needed
    ...
    while True:
        # you will now loop through this indefinitely, please put delay statement
        # after to not run this as fast as possible
        delay(1)
        # code goes here
        ...

If you want to break out of the loop, you can use:

if condition:
    break

br,
Lefa

Hey @Lefa, do i need to alter the code in anyway or can I add the code to what i have and it should work?
regards

Hi,

You can wrap the the needed code that you want to permanently loop inside the while True loop. Please remember to add the delay unless your own loop already has time consuming actions, such as servo.move(). Just keep in mind that the code snippet is a “template” albeit it should work on the fly. Replace the “…” parts with your code and check that the tabulation is correct.

br,
Lefa

Hey,
added the code and getting incorrect syntax error codes? not sure why

Check your tabulation and error messages and please post them

Hey sorry not syntax codes, its an indentation error
image

Hi

Unlike it some other coding languages, Python uses tabulation as its way to mark “code blocks”. The common case of using code blocks is to tell the compiler/translator “what part is this piece of command” executing. Often other code languages wrap their code blocks inside {curly_brackets}. For example in pseudocode:

if (some_condition) {
    // execute this piece of code
    ....
    // also, no tabulation needed, but it makes it super easy to read!
}

In Python, if you don’t have proper tabulations, you get the indented block error, for example in Python:

if some_condition:
    # tabulation is necessary
    ...

if you write something in Python like this…

if some_condition:
print "1"
    print "2"

you will get a “expected indented block”, in pseudocode it would look like this

if (some_condition) 
print "1"
{print "2"}

and the whole thing would explode. I hope this clarifies a tiny bit.

br,
Lefa

Hey @Lefa,
It kind of makes sense im new to coding still wrapping my head around it all, I appreciate the help
regards

Hey,

Happy to help, and it looks like that I’m using tabulation and indentation as if they were synonymous. In Python editor, you can just click “tab” and it brings your indentation level at the right spot (or just gives you 4 spacebars worth of white space and groups them as one).

1 Like

Thanks for the information
regards

Hi all,
I tried working on the code still ran into the same error however, i changed a few things and getting no error codes but the robot doesn’t move can anyone assist on why this is happening

Hy,

Regards
Feature

Hy,

and by the way I think thats always true…

Regards
Feature

Hey @captain_feature fixed the code it was 'white space ’ before the code and now i have a name error thank you for the assist, and if i change it it stops half way through the code

Hy,

work with tab instead of whitespaces could avoid searching for bugs…

Regards
Feature

1 Like

Appreciate the advice

Not sure why the error code is coming up this is the name of the block it placed it at ?
image

Hy,

where is block declared?

Regards
Feature

Just appove the while true statement
image