Hello! I have a code that runs on the row range, while throwing out None
. If you do not use RETURN
and DEF
, everything works fine, but I can not pick up the obtained values from the function to further code. Example:
for i in range (19, 24):
model_name = (sheet.cell (row = i, column = 2) .value)
If model_name! = None:
Print (Model_Name)
Conclusion:
value 1
Meaning 2.
...
Decided to try using def
+ Return
, but it turns out that only the first value from Range
is closed. Example:
def md ():
For i in Range (19, 24):
model_name = (sheet.cell (row = i, column = 2) .value)
If model_name! = None:
Return Model_Name.
Model_Name = MD ()
Print (Model_Name)
Conclusion:
value 1
Question: how to make it possible to pull out all the values from the function in the “global” code, i.e. beyond her? Thank you all in advance!
p.s. It is also necessary to drain none
Answer 1, Authority 100%
You can do without an intermediate list if you use the Yield
statement, with which you can return from the function as many values (including infinite number of values):
def get_models ():
For i in Range (19, 24):
model_name = (sheet.cell (row = i, column = 2) .value)
if model_name is none:
Yield Model_Name.
For Model_Name in Get_Models ():
Print (Model_Name)
Answer 2, Authority 50%
Return list.
Example:
def md ():
RESULT = []
For i in Range (19, 24):
model_name = sheet.cell (row = i, column = 2) .Value
If Model_Name Is Not None:
Result.APPEND (Model_Name)
Return Result