Using multiple Python inputs

Hi,

I’m trying to check four values with this Python script. However this script only checks the value from the fourth input. My Python knowledge is very limited. How can I ensure the script checks all the input values and enters them in the output? I want to use these values as input for other nodes. Is it possible to get these values as four separate outputs instead of one?

Thanks in advance!

As simple as that:

a = IN[0]
b = IN[1]

You can’t have separate outputs but you can output them as list:

OUT = [a, b, c]
1 Like

Hey,

So your first question… The reason you’re just getting the last item outputted is that you’re creating a variable dataEnteringNode, then you are setting it’s value to IN[0], then you are changing it’s value to IN[1] etc. so when you run your test, it is against the final value you have set it to… 400.

Tomasz solved the 2nd part of your question.

I’m afraid there’s quite a lot wrong with your Python (and I’m no expert).

I recommend the free course at CodeAcademy https://www.codecademy.com/learn/learn-python or if you have a Lynda account, there is a course there also.

Here’s my version of what you’re kind of trying to do… I’ve had to adjust it a bit to make sense…

You’re feeding in several values (usually in Dynamo you’re feeding in 1 list of several numbers). Usually when you do this you define each of the inputs separately. Then you need to add them to a list and test them sequentially. I’ve shown the usual way (Append) but also a way of adding several values (extend).

Edit: As noted below, because every input is being used in the For Loop, you don’t need num0 = IN[0] or append etc. etc., you can just type listnums = IN and use that list. However this is an unusual case.

Other things to note…

  • An Elif needs to be terminated by an Else.
    Edit: as described below this is incorrect, however for the purposes of this code, it is more efficient to use else.

  • You’re feeding in numbers upto 400, but you’re testing for anything which is over 400?

  • You have put your output in which is why it’s coming out as a list. In order to run the For Loop the list was already created, so you can just output y

I hope that helps,

Mark

Hi Mark :slight_smile:

I don’t think this is correct actually, in the same way that you can initiate a If statement without having Else statements afterwards. (However, it is true that it is necessary if you want to have lists of same lengths)

Edit : another way to achieve the initial problem. Note that the variable IN already is a list of all the inputs

1 Like

Thanks :slight_smile:

Ok, would you agree that it’s bad form? Why finish with an Elif statement if you can just say Else? :stuck_out_tongue:

Some times you want to operate two set of operations on two different sets of items, and operate none for all the other items.
Say for instance you want to double the value of all the negative numbers, triple the value of all the odd positive numbers, but do no operations on all the even positive numbers.

Two possible solution here :

  • If(negative) : blabla ; If(positive and odd) : blabla
  • If(negative) : blabla ; Elif(odd) : blabla

Note that If I would have used Else in the second solution, I would have operated on all the other items that did not match any of the previous criteria, which is something that we don’t want to do here.

1 Like

Great, thanks :smiley:

Glad I could be of some help :slight_smile:

I tried to get a working version that was as close as I could to what the OP had written and also one which is typical of how people structure code when they are beginners (like me :slight_smile: ).

You have written a beautifully concise version, but it’s a bit more obscure :smiley:

I hadn’t realised that you don’t need to define the inputs in this instance, that’s a lightbulb… It’s also interesting that in this single line style, you don’t need to declare the list before using it, because you are defining it as you use it, you never call it.

Thanks again,

Mark

Haha thanks :slight_smile: I learned some tricks here and there :stuck_out_tongue:

It could be even more concise by not naming IN and using it directly in the definition of the list

1 Like

Well sure, but that’s freakin’ impossible to understand :stuck_out_tongue:

OUT = [min(400,i) for i in IN]