Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

self defined Function does not work – probably mistake in understanding how they work (Python)

I am trying myself in functions right now to make my code shorter. Unfortunetly when I ran the code I get a name error with the variable I want to calculate in the function.
My goal is to calculate the row number depending on the value of the cycle variable.

def row_number_calculater(cycle):
    if 1 <= cycle <= + 40:
        row = 0
    elif 41 <= cycle <= 80:
        row = 1
    elif 81 <= cycle <= 120:
        row = 2

    return row

later in the code I use the function like this ( this is just a cut out of the code):

for i in data:
    cycle +=1
    row_number_calculater(cycle)
    if i == "noop":  
        if ((cycle-1)-(row*wide)) == X or ((cycle-1)-(row*wide)) == (X-1) or ((cycle-1)-(row*wide)) == (X+1):
            Picture[row][(cycle-1)-(row*wide)] = "#"
        else:
            Picture[row][(cycle-1)-(row*wide)] = "." 

Anyway I get the a Name error for row. Now I wonder what I missunderstand in how the functions work… because somehow it does not safe the variable row that I can work with it later on.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am happy for any help!

>Solution :

You are not storing the value of the row when calling the func, use:

row = row_number_calculater(cycle)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading