Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] String input problem

edited May 2013 in OpenSesame

Hi!

I'm doing a test were the participants inputs a string of numbers. I borrowed your code from this thread and got it to work as intended except for one case. The repsonse seem to be interpreted as a value so zeros in the without preceeding numbers in the response are discarded. I've tried handling the input as a string but can't figure out what I'm doing wrong.

from openexp.canvas import canvas
from openexp.keyboard import keyboard
my_canvas = canvas(self.experiment)
my_keyboard = keyboard(self.experiment)
resp = str("") # Here we store the response
while True:
    # Get a keyboard response
    key, time = my_keyboard.get_key()
    # keyboard.to_chr() converts the keycode to a description,
    # like 'space' or 'return'. If return is pressed the loop
    # should be exited.
    if my_keyboard.to_chr(key) == "return":
        break           
    # Handle backspace by removing the last character
    if my_keyboard.to_chr(key) == "backspace":
        resp = resp[:-1]    
    else:
        # The built-in chr() converts the keycode to a character,
        # like ' ' and '/'.
        resp += my_keyboard.to_chr(key)
    # Copy the canvas from a sketchpad called 'my_sketchpad' and
    # overlay the response
    my_canvas.copy(self.experiment.items["my_sketchpad"].canvas)
    my_canvas.text("mata in de 4 sista siffrorna", x=0,y=-64)
    my_canvas.text(resp)
    my_canvas.show()    
# Save the response
self.experiment.set("response", resp)

Comments

  • edited May 2013

    Hi Benno,

    Welcome to the forum!

    Indeed, OpenSesame automatically decides what type a variable is, as described here:

    In this case, this is actually not what you want, because when OpenSesame decides that a variable is numeric, the leading zeros will be lost. There are various ways around this, for example by prepending a '_' to the response:

    self.experiment.set("response", "_%s" % resp)
    

    In this case, 0001 will be saved as _0001, and OpenSesame will not think it's an integer. Whether this is a suitable approach depends on how you intend to use these responses later on. If it's just for logging purposes, prepending an '_' should be fine, I think.

    As an aside, forms provide a simpler way to collect text input. Depending on what you want to do, you could consider using a form instead of this inline script (but whatever works for you, of course).

    Good luck, and let me know if this doesn't answer your question.

    Cheers!
    Sebastiaan

  • edited 1:53AM

    Your sollution works well for my problem.

    Thanks for the quick response. :)

Sign In or Register to comment.