This Question have 3 answers right now.

Python variables in functions? [duplicate]

Question!

I have a little question about variables. My main language is Java (and I'm learning Python) so, I have a problem calling a variable in a function, it doesn't refresh its new value:

# Values
global e1x, e1y, e1c, e2x, e2y, e2c, altx, alty, x, y

def getValues():
    print("Taking Ax + By = C:")
    e1x = float(input("Value of x in first equation: "))
    #...
    if(confirm()): # A function I ommited 'cause its irrelevant
        return e1x, e1y, e1c, e2x, e2y, e2c
    else:
        getValues()

def calculateValues():
    # Stuff with variables


# MAIN
getValues()
calculateValues()

I tried to write it without the global, tried to use the self word but, It doesn't work. (Using Python 3)

ERROR:

 Traceback (most recent call last):
   File "E002_GaussSeidel.py", line 41, in <module>
      calculateValues()
   File "E002_GaussSeidel.py", line 34, in calculateValues
      print(str(e1x))
 NameError: name 'e1x' is not defined


Answers

You need to include global within your function. Outside it does nothing.

def getValues():
    global e1x, e1y, e1c, e2x, e2y, e2c, altx, alty, x, y
    print("Taking Ax + By = C:")
    e1x = float(input("Value of x in first equation: "))
    #...
    if(confirm()): # A function I ommited 'cause its irrelevant
        return e1x, e1y, e1c, e2x, e2y, e2c
    else:
        getValues()

def calculateValues():
    # Stuff with variables


# MAIN
getValues()
calculateValues()

But why do you need global variables? Are you going to use those variables outside your function? global is only necessary if you need to modify values that exist outside the scope of the function.

Reformat your code to be like:

def getValues():
    print("Taking Ax + By = C:")
    e1x = float(input("Value of x in first equation: "))
    #...
    if(confirm()): # A function I ommited 'cause its irrelevant
        return e1x, e1y, e1c, e2x, e2y, e2c
    else:
        getValues()

def calculateValues(values):
    # Stuff with variables


# MAIN
calculateValues(getValues())

Instead of passing information with global variables, this passes information by return values. There are hundreds of articles on why global variables are evil.

values holds the returned variables e1x, e1y, e1c, e2x, e2y, e2c. It is accessible using list index notation. If you want to refer to the variables by their names, use:

#...
def calculateValues(e1x, e1y, e1c, e2x, e2y, e2c):
    # Stuff with variables


# MAIN
calculateValues(*getValues())

*foo is list unpacking notation. It is an advanced topic, but it is useful in your situation. You can read more about list unpacking here.



Select the count into a variable and then use that in the comparison.

DECLARE total_row INT DEFAULT 0

START TRANSACTION;

INSERT INTO fields (field_name, control_type_id, needs_approval)
VALUES ('Array Photos', 3, 0);


SELECT count(job_type_name) FROM job_types WHERE job_type_name = 'Cash' INTO total_rows;

IF total_rows =2 THEN
    ROLLBACK;
ELSE

    INSERT INTO field_to_job_type (field_id, job_type_id, sequence_number, parent_id)
    VALUES (last_insert_id(), (SELECT job_type_id FROM job_types WHERE job_type_name = 'Cash'), 1, (SELECT field_id FROM fields where field_name = 'Photo Pack'));
   COMMIT;
END IF;
By : e4c5


You can use a WHERE clause for this. Convert your INSERT VALUES to an INSERT SELECT and add a WHERE clause.

For example,

INSERT INTO fields (field_name, control_type_id, needs_approval)
SELECT
'Array Photos', 3, 0
WHERE Condition;

If Condition is true, it will insert the row. If Condition is false, the SELECT will return zero rows and thus the INSERT will insert zero rows.



Video about Python variables in functions? [duplicate]