Ultrasonic, line follow, and bumper

I am still new to all of this but I am a teacher and I have a bunch of the vex cortex, and there is no way that I can afford to upgrade to the V5 at this time and there is nothing wrong with the cortex that I have. With that being said, there seems to be a lot of tutorial material for the V5 but not as much for the cortex.
What I am trying to do is get three basic sensors running, but what I have tried so far is not working. I would like to get the ultrasonic sensor working, the line finders working, and the bumper switch to work.
This is the program that I have. There are no syntax errors, but it is not working. Can anyone please advise on what I might be doing wrong?

Hi Shodai.

It is very helpful for larger projects to link to the project page, but for short examples you might want to include the code in your forum post, as the project may be changed later. For example, when I looked at the linked project earlier, you had some code along the lines of:

if bumper_1.is_on: #Subtly incorrect!
   print "Bumper on"

It is important to note that code can compile and run correctly, with no syntax errors at all, but still do unexpected things. Syntactic correctness is necessary but not sufficient.

Here the problem is that you are missing the parens that will call the function method is_on. Your code should be:

if bumper_1.is_on():
   print "Bumper on"

This executes, or “calls”, the method, which is what you wanted. Dropping the parens will do something confusing: the if statement will always run, like the bumper switch is constantly being pressed.

This is because leaving the parens off just names the bumper switch class method, instead of calling it. Handing it to the if statement will cause it to always execute because the if statement tests “truthyness”, and objects are considered to be “true”.

If you are new to Python, and need to get something done quickly, we strongly recommend using Blockly and viewing the generated code. Here is an example program that prints “Bumper is on” using Blockly. The generated code is shown in the “Generated Code” tab.

The Blockly code generator makes it a little harder to generate invalid code, and can be used to quickly try out sensors.

You can also view the RMS Python Cortex API docs here: https://www.robotmesh.com/studio/content/docs/vexcortex-python//html/namespacevex.html

Cheers, Sam.

Thank you so much. The API is something that I have been looking for but could never seem to find. This is unbelievably helpful.