Sign in with your CogSci, Facebook, Google, or Twitter account.

Or register to create a new account.

We'll use your information only for signing in to this forum.

Supported by

attach variable to string

PiaPia Posts: 32


sorry for asking many questions, but I'm new to Python.
So, I have an inline-script which sends triggers to the serial port, like this:
These triggers show then up in the logfile of the program that is connected via the serial port (which is an eye-tracker, but I think this doesn't matter for my question).

What I would like to do is: Instread of just "overview" (or "summary" or "draw") I would like to send "overview" and a variable, so that I know which overview (or which summary or which draw) it is. I have these variables defined in a loop and would like to attach them to the string which I send (like port.sendMessage("overview"&var.jar)) but I don't know how I can do that.

Can you give me any tips how to do this or where to look it up?

Thanks a lot!


  • JoshJosh Posts: 361
    edited May 2017

    Hi Pia,

     port.sendMessage("overview " + str(var.jar)) 


    Thanked by 1Pia
  • PiaPia Posts: 32

    Thanks a lot!!

  • PiaPia Posts: 32

    Thanks again. Now I have another, very much related, problem.
    So some of my variables have this format (in the inline-script):
    (So the variable names are color_of_draw1, color_of_draw2, and so on.)
    I try to send those to the serial port aswell. Here is how I tried that:

    color_of_draw_var = "color_of_draw" + str(var.jar_nr)
    port.sendMessage("Draw Jar" + str(var.jar_nr) + "Draw Marble" + str(var.color_of_draw_var))

    So, as you see, I tried to create a variable color_of_draw_var which I then use. But it doesn't work. It sais "The variable 'color_of_draw_var' does not exist."

    How can I create that variable?

  • eduardeduard Posts: 1,060

    Hi Pia,

    you create a variable called color_of_draw_var and in the next line you use a variable called var.color_of_draw_var. Those are not the same, you see?


    Thanked by 1Pia
  • JoshJosh Posts: 361

    Hi Pia, color_of_draw_var (first line of your code) is unequal to var.color_of_draw_var (second line of your code)


    Thanked by 1Pia
  • PiaPia Posts: 32

    Thank you! It works (I didn't know that I didn't had to put the ´´var.´´), but I noticed it's not what I wanted.

    So, if I do it like this:

    color_of_draw_var = "color_of_draw" + var.jar_nr
    port.sendMessage("Draw Jar" + str(var.jar_nr) + " Draw Marble " + str(color_of_draw_var))

    It prints for example (if the Jar was 2) color_of_draw2. I see that that makes sense, but I didn't want it to print color_of_draw2 as a text but the contents of my variable called color_of_draw2.

    Here are my variables:

    And I would like to print the contents of those (to the serial port).

    Any idea how I can do that?

  • eduardeduard Posts: 1,060


    color_of_draw_var = "color_of_draw" + var.jar_nr


    color_of_draw_var = exp.get("color_of_draw" + var.jar_nr)

    Thanked by 1Pia
  • PiaPia Posts: 32

    Thanks a lot, it works!! :smiley:

Sign In or Register to comment.