For loop in def with 2 argumenst

#1

Hello,

i’m kind of new to python, perhaps someone can point me in the right direction?

I’ve created a definition with some if statements inside. Now when the input is a list I get only 1 value as output, right? So how can I create a for loop in a definition with 2 arguments that will return me the value I need for each value in the list?

#2

line 26, what are the values of `x` and `y`? A function can only return a single value. So then you would need to return a list. Which means make a for loop in your function to create the list with all the values you need

also, Boolean values in python are case sensitive, `False` is the Boolean value.

#3

Hi,

thanks for the response.
The values for x and y are in line 7 and 8. In dynamo environment I’ve 2 vectors as input (see picture below). and the input of these two values are both lists.
Yes, that’s what I thought, but i’ve no clue where to start with this for loop. Does it has to be inside the def vector_degree?
And the values of x and y will return a value in the definition, right? So the outcome of 'vector_degree (x,y) will be 0, 90, 180, 270 or false (or False, thank you for mentioning that)
So where to start with this for loop? I’ve really no experience with loops at all, so a little help would be much appreciated

#4

i saw that, but i have no idea what `IN` for value has

then the for loop should be outside the function.

But what are you going to store in `OUT` then? Currently its an integer, but integer can only be a single value, shouldn’t OUT become a list? Then we can place the loop in the function

#5

exactly, here’s another image to clarify it better
So i’ve 2 lists, x and y, i’ve 5 conditions for returning a value, like:
if x >= 0 and y >=0: return 0

Then there should be as many outcomes as there are values in list x and y. So how can i put this ‘for loop’ inside the function, can you help me with that?

#6

you could simple create a loop inside the function:

``````result = []

for i, j in zip(x, y):
print(i, j)
``````

now `i and`j will contain the values, so we can use those for the comparison operator and then `.append()` to `result`

#7

Creating a loop like that ought to be rather trivial, what’s stopping you exactly? Iterate over your list and call the function, right?

Also, I suggest not returning False from your function, this could be interpreted as the value 0, which is also a valid result. Instead your function should probably either raise an exception (crash) or return None (no result)

And your function itself should probably only operate on two numbers, call it multiple times rather than putting code in that function which doesn’t really belong there

#8

precisely, well I know how to do a list with a single argument:

vectors: [2,5,7,8]

for value in vectors:
if value > 0:
return value

something like that, right? But now I’ve two values, and want to do a couple of if statements in the loop to return different values based on both the arguments.

so how can i do this? With dynamo (a visual programming environment) i’ve make a node what does exactly what I want, but i want to know how this is done in python.

#9

i showed the use of `zip()`? Return is the last thing a function does, so a return keyword will end the function. So you might want to return a list.

#10

You mean apply your function, right.
You’ve made a function which processes one pair. Don’t change that function any further. (except for not returning False since that could hide bugs, it should crash if that happens)

If you have two vectors/lists/whatever, then you could iterate by index, grab x and y and call your function and put the result in a list.

You can write this in more fancy manners using things like list comprehension, zip, map… But again, you only need some rather basic actions to make it happen.

#11

alright, I think I understand, but still not sure how to iterate by index and grabbing x and y. Can you show me?

#12

You’d get the length and start counting up from 0

…Forget about python for a while and simply ask yourself what it is you want to do. The above suggestion is probably pointing out the obvious. So you’d decide on what, and then you’d do that

#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.