Grasshopper

algorithmic modeling for Rhino

Initializing counter value once a threshold is reached Python

Hi, 

I am trying to write a very simple code to initialize a counter value for variable Y once variable X (inputted with a number slider) reaches a certain threshold. Also, the Y value should only increase if and when the x value does. This is why I think a while loop might not be the best solution. I tried a 'for each', but I cannot iterate over a non-iterable obviously. 

For instance, once the input of x reaches n values, then I'd like to initialize y and then stop y's counter once it reaches a threshold. Here is my code: thank you

(attempt 1)

y = 0

while x > 10:
y += 0.5
if y >= 4.0:
break

a = y 

(attempt2)


vals = []
inc = x + 0.05

for val in vals:
vals.append(x)

if vals > 10:
y += inc
a = vals

Views: 2145

Replies to This Discussion

Sounds like you're in need of a persistent variable? If so, there are at least two different strategies for doing this within a GHPython component (both covered in this thread). I would recommend the most straightforward one outlined by Giulio here.

Hi,

let me know if this helps. Please find the file attached.

Attachments:

Thank you to both of you. @ Anders - yes, the links you posted are very helpful.
@ Devang, thank you your solution is very elegant. Much appreciated.

Thank you again

Turns out I have another issue which is crashing my system.

I am trying to reverse a number slider input once I meet a threshold with this code:

threshold = x/2
inc = float(.1)

y = 0

while true:
    if y >= len:
    inc *= -1
    y += inc

    if y <= 0:
    break

a = y

I would rather not use a while loop so that I can control the descent with a number slider

Attachments:

Hi,

Please find attached file. Is that what you're looking for?

Attachments:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service