For and while loop in combination, how?

Hello,

I do some basic code again:

X = 1
Y = 2

print("{:>4}".format(" "), end = " ")

for X in range(1,11):
print ("{:>4}".format(X), end = " ")

print()

for X in range(1,11):
print ("{:>4}".format(X), end = " “)
while Y <= 10:
print (”{:>4}".format(X * Y), end = " ")
Y += 1
print()
Y=1


Here are a other example:

i = 2
while(i < 100):
j = 2
while(j <= (i/j)):
if not(i%j): break
j = j + 1
if (j > i/j) : print i, " is prime"
i = i + 1

print “Good bye!”


It should create a list of numbers… …how is the translation regarding Print and OUT? There are other translations f.e. print and return. I try to find the gramma between python, C#, … designScript

How can i transform this to designscript, here are my trys:
for_while_loop_loop.dyn (3.0 KB)
2019-03-26_11h45_09

I changed the “prints” to “returns”, but dynamo is still confused, because the returns at wrong points…!
I think i have to change more the Syntax.

I am happy about any advice

KR

Andreas

loops.dyn (6.6 KB)

Hi,
there are some errors in your code.

  1. Don’t use return - create lists and than insert something in these new lists
    For example with “append”
  2. OUT means output, write list after “OUT=” you want to work outside of the script.
  3. don’t use the variables of the input ( X and Y) in your “for loops”

Hi,

I’m not sure exactly what you want to get… But just to try and clarify…

OUT is everything you want to output… so to ‘print’… make a variable equal the value you want to ‘print’, then output that variable… E.G…

if (j &gt; i/j) : print str(i) + ' is prime'

is changed to

if (j &gt; i/j) : k = str(i) + ' is prime'
OUT =  k

The loops themselves are quite complex, so it’s hard to see what they are meant to be? I’d test on a more simple example until you’re comfortable with them…

If you use append, you’ll see the progression of numbers which might make the process more obvious?

Perhaps have a play with this… FYI, the <> will let you format code… and if you use ’ not " then it will copy and paste into here easier…

i = IN[0]

z = []

while(i < 50):
    j = 2
    z.append(j)
    if not(i%j): break
    i = i + 1


OUT =  z

Hope that helps,

Mark

1 Like

Thank you: that`s a clear statment - Dont use return, use .append!

Also the fakt “to store results” in a “empty variable” !

a = [ ] is a list, a = 0 is a variable :slight_smile:

If you append into a list, you will OUT a sequence…

So a+1 while a<= 5:
If appended into a list OUT = 1,2,3,4,5
If it equals a variable, you will just get the final value, so OUT = 5