[Solved] convert int to str opensesame inline script
Hello :)
I am quite new to opensesame and programming and now i am stuck in a problem with converting an integer to a string in an inline script in Opensesame.
This is my code so far:
var.stritem = str(var.item)
var.itemcode = var.stritem + var.gsk_it
Now i tried two different versions of converting this int, but none has worked. Since I want to generate a new variable that includes the Itemcode, i need to concatenate those two variables.
I hope you can help me, maybe this is a very obvious mistake. 😅
leonie
Comments
Hi @leonie_k,
Welcome to the forum!
I'm no Python expert, but I think that within OS, a variable that contains only numerical characters, even if extracted the
strfunction, is interpreted as an integer. (developers, please correct me if I'm wrong).So, in your code, if var.item is numerical (I assume it is), var.stritem will be interpreted as numerical. Let's say that var.item contains the value 22, then str(var.item) would be a string ("22"), but because it contains no alphanumerical characters, OS interprets var.stritem is as a numerical value anyway, so that var.stritem is actually a numerical variable.
However, if you combine str(var.item) (22) and an actual string (e.g., "test"), then the result is alphanumerical and so the result would be a string ("22test").
Run this Python code in OS and it should work:
# sets up example valuesvalues var.item=22 var.gsk_it="test" # combine integer and string into a string var.itemcode = str(var.item) + var.gsk_it # Print to console print ("Checking the result: "+var.itemcode)Hope this helps!
Fabrice.
Thank you so much! It works!
😀
Hi @leonie_k,
Glad it's working! 👍️
Just so that you know, when messaging on the forum, you can make sure your correspondent receives notification of your message by using their handle. For example, to reply to my messages, if you include @Fab in the message, I'll be notified. As you start typing @F, you'll see a pull down menu from which you can select my handle:
That way, people you reply to get a notification (in my case, otherwise I sometimes miss messages as I'm involved in many threads).
Best,
Fabrice.