# My Bloxter/Python program fails, but doesn't return an error?

Greetings!

I have put together a small program that:

Drives forward until:
a. it comes within a specified distance to an obstacle.
b. It strikes an “invisible” (not visible to the distance sensor), obstacle with its bumper.

If it comes within a specified distance to the obstacle, it:
a. If it is further away than “X” distance, it moves forward.
b. If it is closer than “Y” distance, it moves backward.
c. If it is between distance X and Y, it stops and stands still.
d. If the obstacle moves, either closer or further away, it reacts accordingly.

If it strikes something with its bumper, it:
a. Backs away for two seconds.
b. Calculates a random degree of rotation from 10 to 270 degrees
c. Calculates a random binary value, zero or one, which determines the direction of rotation, either left or right.
d. Executes the move in the desired direction for the desired angular degree of rotation.
e. Continues moving forward after the turn is completed.

The “calculates a random binary value” and “executes the move” are implemented as:

``````        IsTrue_ = random.randint(0, 1)
if IsTrue_:
gpg.turn_degrees(-1*RotateDegrees, blocking=True)
else:
gpg.turn_degrees(1*RotateDegrees, blocking=True)

``````

in Python, (cut and pasted from Bloxter). This little construction is probably the source of my problem.

The big issue is that no matter how I try to execute it, either in Jupyter Lab (as a page), in Python, (by cutting-and-pasting the python from Bloxter - and I’m probably doing it wrong), or in Bloxter itself, the program fails silently  providing absolutely no help whatsoever  to the errant programmer.

I have tried this in both Chrome and Firefox, on both my laptop and a tablet, and there is never any error returned. It simply starts, pauses for a brief moment, and stops. (i.e. the program (in Bloxter), goes into the “working” state for a second or two, then returns to the “stopped” state.)

The Musical Question(s) are:

1. Why does this happen? (i.e. what is the actual error here?)
2. (More important IMHO) - Why is there no error reported? Are programming errors supposed to be reported in Bloxter? I cannot even get one in cut-and-paste into “programming in Python”.

Thanks!

Jim “JR”

Attachments:

1. Distance_Bumper_RandomDirection.blox
2. Distance_Random_Direction.py

Distance_Random_Direction.py (2.1 KB)

Looking at the code you have provided, it certainly doesn’t make any sense for it to exit without any notice. Maybe I’m missing something. I’m also assuming the GoPiGo3 perfectly functional.

Have you tried adding prints everywhere and see where the program exits? If you think the program exits after entering the while loop, then just comment everything in the loop except the sleep call and see if it still exits. If it does exit, then start adding prints within that loop.

Thank you!

I’m going to try exactly that when I take my next break from taxes.

What puzzles me is not so much that it fails – with my “true-false” kludge, I was almost expecting a fail – but that it doesn’t report any error. (head-scratching smiley)

I walked through the logic and found that after button is pressed it backs up, rotates and then never moves again unless the button is pressed, in which case it will rotate again. It never moves forward again. I don’t see any syntax errors but don’t have a GoPiGo with a button handy to test your code.

I replaced the gpg commands with print statements and see the following behavior:

``````stopping
eyes are white
going forward
is button pressed? Y/n
n
is button pressed? Y/n
n
is button pressed? Y/n
Y
What is distance ?
11
eyes are Yellow
is button pressed? Y/n
n
What is distance ?
11
eyes are Yellow
is button pressed? Y/n
n
What is distance ?
8
eyes are Red
is button pressed? Y/n
n
What is distance ?
15
eyes are Blue
is button pressed? Y/n
Y
going backwards
stopping
rotating left 31 degrees
What is distance ?
11
eyes are Yellow
is button pressed? Y/n
``````

So while your logic may not do what you want, it looks fine. I would check the gpg calls to make sure they all work properly. Consider adding more eye color changes and/or use the turn signals to show where the code flows as well (or prints if you are running the python version).