Hi everyone! I am quite new to the forum and this is my first time posting. I am using Windows 10 and PsychoPy 3.8 and I am working on letting participants fill in a number between 0 and 1000. I am using the following code, where text_neutral is my empty text box. It seems to work partially but I cannot get it to work so that you cannot fill in the numbers “1001” “1002” “1003” and so on. Participants are also seeing what they are typing in.
if len(keys):
if len(text_neutral.text) > 2:
if (text_neutral.text) == “100”:
continueRoutine = True
if len(text_neutral.text) > 3:
if (text_neutral.text) == “1000”:
text_neutral_text = text_neutral.text[0:3]
else:
text_neutral.text = text_neutral.text[:-1]
else:
text_neutral.text = text_neutral.text[0:2]
if len(text_neutral.text) > 3:
text_neutral.text = “1000”
if len(text_neutral.text) > 4:
text_neutral.text = text_neutral.text[0:2]
elif ‘backspace’ in keys:
text_neutral.text = text_neutral.text[:-2]
elif ‘return’ in keys and len(text_neutral.text) < 1:
continueRoutine = True
elif ‘return’ in keys and len(text_neutral.text) > 4:
continueRoutine = True
elif ‘return’ in keys and len(text_neutral.text):
allowed_keys = []
else:
text_neutral.text = text_neutral.text + keys[0].replace(‘num_’, ‘’)
There was no specific error that came up, I just cannot get it to stop when somebody tries to tick in “numbers” between 1001 and 1009.
if len(keys):
if len(text_ss1.text) > 3 and (text_ss1.text) != "1000": # remove the last digit if the number is longer than 3 digits and it is not 1000
text_ss1.text = text_ss1.text[:-2]
if ('backspace' in keys): # ceck for backspace
text_ss1.text = text_ss1.text[:-2]
elif 'return' in keys and len(text_ss1.text) > 1: # check for return and end the routine if at leas one digit has been entered
continueRoutine = True
else: # add to the number that is displayed
text_ss1.text = text_ss1.text + keys[0].replace('num_', '')
Thank you @LukasPsy for your answer and for taking the time to look at it.
When I implement this, the very same problem occurs. I, as a participant, can insert values ranging from 1001 to 1009: 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009.
I actually think I tried using this exact code myself before as well:
if len(text_ss1.text) > 3 and (text_ss1.text) != "1000": # remove the last digit if the number is longer than 3 digits and it is not 1000
text_ss1.text = text_ss1.text[:-2]
It would make sense for it to work, but somehow I can still fill in “1001” and so on.
Okay, I must have overlooked something. Could you try this:
if len(keys):
if ('backspace' in keys): # check for backspace
text_ss1.text = text_ss1.text[:-1]
elif 'return' in keys and len(text_ss1.text) > 1: # check for return
continueRoutine = True
else: # add to the number that is displayed but only if the new key does not make the number larger than 1000
newNumStr = text_ss1.text + keys[0].replace('num_', '')
if int(newNumStr) <= 1000:
text_ss1.text = newNumStr