How can I make sure an user input is a number for some questions in the loop and a string for others

Skip to first unread message

Nanda Zettel

unread,
Apr 11, 2014, 2:46:47 PM4/11/14
to psychop...@googlegroups.com
Hi, I just updated my psychopy to the new version released this March. I am new to psychopy and python and have been blessed with help from the forum and I am very thankful. I was helped with writing some code to allow users to type in answers coming from an excel sheet and presented to the user as a loop in a routine. My current issue is that I need to check if some of those answers are just numbers and other answers should be a regular string. So I have had this variable called "captured_string+cursorVariable" and I created a new one called "captured_number+cursorVariable" which is to be the one that will only allow numbers to be typed in. I tried using the built in "isdigit()" python function but I am not sure where to put it and it's not working. 

I have the following code in a code component in my experiment:

BEGIN ROUTINE:

cursorCounter=0
cursorVariable='|'
captured_string=''
captured_number=''
subject_response_finished=False
entered_number_finished=False
trial_clock=core.Clock()

EACH FRAME:


if cursorCounter >= 30:
    if cursorVariable=='|':
        cursorVariable=' '
    else:
        cursorVariable='|'
    cursorCounter=0
cursorCounter+=1

#CODE FOR STRING INPUT:(USE VARIABLE captured_string+cursorVariable in Excel file)
if subject_response_finished:
    final_response=captured_string
    continueRoutine=False

#CODE FOR NUMBERS INPUT: (USE VARIABLE captured_number+cursorVariable in Excel file)
if  entered_number_finished:
    final_response=captured_number
    continueRoutine=False

for key in event.getKeys():
    if key in ['escape']: 
        core.quit()
    elif key in ['delete','backspace']:
        captured_string = captured_string[:-1] 
        captured_number = captured_number[:-1] 
    elif key in ['space']:
        captured_string = captured_string+' '
    elif key in ['period']:
        captured_string = captured_string+'.'
    elif key in ['comma']:
        captured_string = captured_string+','
    elif key in ['apostrophe']:
        captured_string = captured_string+"'"
    elif key in ['slash']:
        captured_string = captured_string+'/'
    elif key in ['minus']:
        captured_string = captured_string+'-'
    elif key in ['return']:
        subject_response_finished=True
        entered_number_finished=False
    elif captured_number.isdigit() and key in ['return']:
        entered_number_finished=True
    elif key in ['lshift','rshift','up','down','left','right']: 
        pass 
    else: 
        captured_string = captured_string+key
        captured_number = captured_number+key
    # this next line formats the output. you can remove or modify as necessary
    captured_string=captured_string.capitalize()


I have attached my test experiment in a zip file and also individually. Any help will be greatly appreciated! I need to format the captured_number to only accept numbers and have captured_string for the other response types.

Thank you so much for your time and attention!

Fernanda.
TestConditions.zip
FASM_test.xlsx
TestConditions.psyexp

Jared Roberts

unread,
Apr 11, 2014, 3:28:52 PM4/11/14
to psychop...@googlegroups.com
Hi Fernanda,

Strings in python have a function called isdigit() that returns True if the string is a number, and false otherwise.  So you could do something like:

if  entered_number_finished:
    if captured_number.isdigit():
       final_response=captured_number
       continueRoutine=False



--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/dfe2960f-ee32-4d0c-b20f-99d5ec4fbb2c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jared Roberts
Graduate Student
Neuroscience of Memory, Aging and Dementia Lab
Department of Neurobiology and Behavior
University of California, Irvine
"A lost cause can be as spiritually satisfying as a victory." - Robert Heinlein

Nanda Zettel

unread,
Apr 15, 2014, 3:03:25 PM4/15/14
to psychop...@googlegroups.com
Thank you so much for your response. Unfortunately, it didn't work with the rest of my code. It still allows strings in the field. I'm guessing the problem may be elsewhere in that same block of code. I have the experiment attached in my original post if anyone could be so kind to look at it. I imagine it will be something simple for someone more familiar with python and psychopy.

Thank you so much for your time and attention!
Any help will be greatly appreciated.

Fernanda :)

Nanda Zettel

unread,
Apr 15, 2014, 3:58:50 PM4/15/14
to psychop...@googlegroups.com
If I use this code:

if subject_response_finished:
    if captured_number.isdigit():
        final_response=captured_number
        continueRoutine=False

The problem is, if the user types in a 1 digit number, it ends the routine if I use the isdigit() method, so the participant doesn't get the chance to type a 2 or more digit number. I need to have the person end the routine when pressing enter but only if they typed a number for the captured_number questions in the loop - or just with the return key if the question requires a regular string response (captured_string). I dont understand why the original code I posted with a variable for the number using isdigit() and one for the regular string won't work. I have the impression it should be pretty close to working but I am new to python and psychopy so I could be wrong. Either way, any help will be greatly appreciated!

Thank you!

Fernanda. :)

On Friday, April 11, 2014 11:46:47 AM UTC-7, Nanda Zettel wrote:
Reply all
Reply to author
Forward
0 new messages