Functions.dyn (9.3 KB)
Hello Dynos,
I try to define Funktions (def), I have agiain some basic issuses:
I try to give strings a value f.e. “B” has 10 or “A” has 5.
For now I have no syntex-error, but the result is 0
KR
Andreas
Functions.dyn (9.3 KB)
Hello Dynos,
I try to define Funktions (def), I have agiain some basic issuses:
I try to give strings a value f.e. “B” has 10 or “A” has 5.
For now I have no syntex-error, but the result is 0
KR
Andreas
you aren’t returning anything in your function.
Plus
you are defining “count” as variabel of your function but in your function you are using “letter”
So with count = 0 you are saying:
Dynamo run me the score function (score(0)).
A few things. What @3Pinter said as well as that you are not adding count to anything so it will only return the final outcome once you do iterate through the letters, which is 0. Also, because count
is inside the function, it becomes a local variable. Basically that means the variable only exists within the function and has no meaning outside of it. It is also disconnected from the original count outside of the function.
I think this is more of what you are going for:
letters = IN[0]
count = 0
def score(l):
if l == "B":
return 10
elif l == "A":
return 5
else: return 0
for letter in letters:
count += score(letter)
OUT = count
LOL! EXACTLY as I was gonna suggest, including the letter/letters
letters = IN[0]
count = 0
def score(letter):
if letter == "B":
count = 10
elif letter == "A":
count = 5
else:
count = 0
return count
for letter in letters
count += score(letter)
OUT = count
Ok and for collecting the results I use not count.append(letter) so I use an Operator +=
Haha that’s great. I actually wanted to suggest using a dictionary for this as it would be more organized:
letters = IN[0]
points = {"A" : 5, "B" : 10}
count = 0
def score(l):
if l in points:
return points[l]
else: return 0
for letter in letters:
count += score(letter)
OUT = count
And how to count separatly? Item by item f.e. 5,5,5,5,10,10,10 count[letters]
If you want to add to a list, first you have to initialize a list and then you can use .append() to append the result of the function.
letters = IN[0]
points = {"A" : 5, "B" : 10}
count = [] # initializing an empty list so you can append to it later
def score(l):
if l in points:
return points[l]
else: return 0
for letter in letters:
count.append(score(letter))
OUT = count
or shorter version:
letters = IN[0]
points = {"A" : 5, "B" : 10}
def score(l):
if l in points:
return points[l]
else: return 0
count = [score(letter) for letter in letters]
OUT = count