How to correctly assign local variables to a for-loop in python?

sudonym

I am having a for-loop which takes rows within a pandas dataframe df_drinks and uses them as parameters to call another function order(). order() is imported from the module restaurant.py.

Together with the row in df_drinks, I want to submit a comment to the order(), which is specified outside the for-loop.

from restaurant import order

statement = "Lets order"
df_drinks = ["1 drink", "2 drink"] # simplified, 1 item per row, many columns

for index, row in df_drinks.iterrows():
    print ("%s, %s" % (statement, row))
    item = row
    response = order(statement, item)
    ...

The module looks like this:

# restaurant.py

def order(statement, item):
    listen(statement)
    statement = "order received"
    ready_drinks = prepare(item)
    ...
    return ready_drinks

For the first run/row everything is fine since a print yields:

Lets order 1 drink

However, for the second run/row, print yields:

order received 2 drinks instead of Lets order 2 drinks.

I understand that I have the same variable name statement for two different things. Still, I am confused since order() in restaurant.py does only return ready_drinks and not statement.

How to correctly assign local variables to a for-loop in python?

Sandeep

statement = "order received" is a local variable and statement = "Lets order" is a Global variable. You are no where overriding the value in the for loop. May be below code helps you

df_drinks = ["1 drink", "2 drink"] # simplified, 1 item per row, many columns

for row in df_drinks:
    statement = "Lets order"
    print ("%s, %s" % (statement, row))
    item = row
    ready_drinks,statement = order(statement, item)
    print ("%s, %s" % (statement, row))


def order(statement, item):
    listen(statement)
    statement = "order received"
    ready_drinks = prepare(item)
    ...
    return ready_drinks,statement

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to assign dynamic variables in a for loop in python

python - Create variables in loop then assign

How to assign multiple variables in a loop for graphs in Python 3

how to assign multiple localstorage variables inside a loop?

How to assign two variables in a single for loop?

How to assign multiple DataFrame variables using a for loop?

How to assign more than 26 variables with FOR Loop?

How to assign different variables in loop in ruby?

How to assign variables on a while loop in Java

How to correctly declare/assign values for variables in c++

How can we assign new variables after each for loop iteration in python?

How does the java compiler assign index's in the local variables table?

How can I assign global and local variables in node js

Linq: How to assign local variables within linq from array

How can I assign local variables in node js

How to extract correctly local variables from Javascript function?

Python How To Assign 2 Variables in One Part(?)

Python - How to Automatically Assign Variables to Numerous Inputs

How to assign and call variables from a class in python

Python: Assign Flexible Variables That Change Based on Value in Loop

Python SyntaxError: cannot assign to operator - Defining variables as sum of strings in for loop

Unable to re assign local variables

How to assign the result of a for loop to a variable in Python

How to assign different variables to a class object using for loop?

Jmeter - How to assign multiple dynamic values to a variables inside a loop

How to assign each variable in a vector of variables new value using loop?

How to create multiple variables in a loop and assign values in VBA

How can I correctly make a for loop for my variables?

Assign instance variables with local variables - Java