psychopy.org | Reference | Downloads | Github

Numerical stimuli converting to float values for textstim

hi all, cannot figure out how to draw a variable text stim (ie, the value is being drawn from a column in my csv / excel input file on each trial) where the character is a number (like 9) but it reads it in and draws it as 9.0. only having this problem with numerical stimuli, in a textstim box. thought something like str($mystim) could work in the variable box but haven’t had any luck. thank you for any help!!!

OS Mac Big Sur (and also when I play it online on windows)
PsychoPy version 2021.1.4:
Standard Standalone? standalone
What are you trying to achieve?:
show the number ‘9’ instead of showing ‘9.0’

What did you try to make it work?:
converting the csv to excel and back, forcing the data columns to save as strings

What specifically went wrong when you tried that?:
it still showed 9.0

I solved this recently using str(int(var))

Thanks for following up – this works when I have a numerical stim, but my column contains both integers and non-ints. so str(int($myvar)) works if myvar = 9, but not if myvar = H, which it does sometimes. wondering if you have other ideas? if this was pure python I guess I would do something like $myvar if not $myvar.isnumeric() else str(int($myvar)) but that won’t work in a text box. any ideas?

Put your code in a code component Begin Routine tab to set a variable (e.g. msg) and then set the text to $msg

thanks, this worked. for those who see this, .isnumeric() doesn’t work in psychopy for some reason (??) so you need to do a try: str(int(var)) except: str(var) kind of situation for each variable.