Hello, I am trying to write a Python script in Grasshopper that can you an “if” then “print” statement for the following numbers to convert to letters. Any help would be greatly appreciated.
for forum_python.gh (3.3 KB)
Assigned to grasshopper category
Instead of print s you can try a=“s”. and instead of connecting panel to out you can connect to a
Additionally, you can convert the input into a list and use a for loop to go through the list and append the output to a.
Unfortunately, option one did not work.
Is there any way you could send a screen shot of what option 2 might look like?
Just try a=“s” not a==s
This should work according to me.
Important to have the required string in quotes.
Also you can look up basics of python from w3schools. Really useful resource to get started with python.
Yeah, that way didn’t work either. I’ll go ahead and look at those tutorials. Unless there is something wrong with the input list?
Try right clicking on x and set type hint to list
And use the following snippet
for i in x:
. if i == 18 || i == 18.0:
. . a.append(“A”)
. else
… a.append(“S”)
PS sorry I’m not in front of my PC rn to guide you better.
Partially successful… there are some values that are less than 18 and 22 that are not being translated. Any suggestions?
Hi @Samantha_Garza , how about this?
Graph Space:
Python Code:
__author__ = "Michael Vollrath"
__version__ = "2023.07.13"
#Made With ♥ In Dallas, TX
#Set Component Information
ghenv.Component.Name = "Convert Number To Letter"
ghenv.Component.NickName = "NumLet"
ghenv.Component.Description = "Takes a list of numbers and returns a corresponding letter"
ghenv.Component.Params.Input[0].Name = "Number"
ghenv.Component.Params.Input[0].NickName = "N"
ghenv.Component.Params.Input[0].Description = "Numbers To Check"
ghenv.Component.Params.Output[0].Name = "Letter"
ghenv.Component.Params.Output[0].NickName = "L"
ghenv.Component.Params.Output[0].Description = "Corresponding Letters, A,S,B"
#Make An Empty List
L = []
Other = [] #This one Isn't Really Needed
def num_to_letter(N):
for num in N:
if num == 18:
L.append("A")
elif num == 22:
L.append("S")
elif num < 18:
L.append("B")
else:
L.append(None)
Other.append(None) #This Isn't Really Needed
return L
L = num_to_letter(N)
#Message To Tell You How Many Letters Were Converted/Found
ghenv.Component.Message = str(len(L)-len(Other)) + " Letters Found"
This works perfectly. I ended up taking out:
elif num < 18:
L.append(“B”)
and its exactly what I needed. Thanks.
Happy to help!