[solved] String input problem
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
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:
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
Check out SigmundAI.eu for our OpenSesame AI assistant!
Your sollution works well for my problem.
Thanks for the quick response.