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

Question:

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.

I am happy for any help!

Asked By: chrislmonster

||

Answers:

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

row = row_number_calculater(cycle)
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.