Issue storing a sensor value into a variable in Python

I need help! The error message i get about the second line (in my function) is:Assigning result of a function call, where the function has no return value

def brightness_difference():
    brightness_now=optical_1.brightness()
    brightness_difference=optical_1.brightness()-brightness_now

# Library imports
from vex import *

# Begin project code
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1,1)
    brain.screen.print("The sensor detects a brightness of")
    brain.screen.set_cursor(1,2)
    brain.screen.print(optical_1.brightness())
    if brightness_difference>0:
        brain.screen.clear_screen()
        brain.screen.set_cursor(1,1)
        brain.screen.print("The sensor detects an increase of brightness")
    else:
        brain.screen.clear_screen()
        brain.screen.set_cursor(1,1)
        brain.screen.print("The sensor detects an decrease of brightness")

First of all, I would suggest defining your function below your library imports section, this is common practice, and makes your code easier to read.

Second, your function calls .brightness() twice, I wouldn’t do that; it increases the chances of your function missing a change in brightness. Rather, you can declare two global variables which will store the brightness values of your sensor. By using global variables, you can now get away with only one .brightness() call.

Third, your function isn’t returning anything, that is what the return keyword is used for, it tells your program what the function should return as it leaves the stack.

Fourth, in your if statement, if brightness_difference>0: , if you don’t include parenthesis after the brightness_difference, that section of code will instead be interpreted as a request to get the function object itself… not good. By instead using brightness_difference(), the function is called.

Here is an example of a way this could be implemented:

I purposefully did not make this code copy-paste-able, forcing you to read it :smirk: eivil laugh

BTW, please don’t feel discouraged when (not if) you make mistakes, programming is a complicated skill and takes years to master (and even then, you will never stop making mistakes and learning). You’re doin’ great!

Aristotle famously wrote, “The more you know, the more you realize you don’t know.”

2 Likes

Thank you very much for your advice.
I thought that return was only necessary in C++. I am fairly new to python and thought it was not needed. Your code is elegant and thank you for the time you spent answering my question. Sadly (it might be a bug in my IDE), it did not compile and the error I get is :“assigning result of a function call, where the function has ne return” in the line of code defining the current_brightness in the function

Huh, that is weird. I moved the program to VEXcode V5 (instead of Visual Studio Code) and I am now getting the same error your are (aside from two errors I made).

I honestly have no clue what is going on here, I found the vex.py file, and here is the declaration of the member function brightness():

… And our use-case appears identical (in structure) to the example shown. :person_shrugging:

I am pretty sure this is a bug on the VEXcode V5 side of things, although I could be wrong.

Regardless, I was able to trick it into not throwing a compile error with this bit of code:

hmm

Compile… Python… :face_holding_back_tears:

What I did was create a simple function that will call other functions / member functions. (obj_caller) I really dislike this solution. I feel like I am ither missing something, or VEXcode has a bug?

One mistake I missed the first time, I needed to bring those two global variables into scope with the global keyword.

The above code “compiles” in both Visual Studio Code and VEXcode V5 (Although it seems that the only one doing any linting is VEXcode V5, something may be wrong with my setup)

The linter and “stub” file (meaning vex.py file) used in the VSCode extension is different than that in VEXcode. Stub files are more recent and somewhat more accurate, the linter is whatever you choose as an extension in VSCode. Just because the linter shows an error, the code may often run on the V5 which is what really counts.

5 Likes