Digital Countdown

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): yes
PsychoPy version (e.g. 1.84.x): 2022.1.1
Standard Standalone? (y/n) yes If not then what?:
What are you trying to achieve?: I’m pretty new to psychoPy. I would like to show my participants a simple digital countdown which starts at 8 minutes and counts down to 0.0. That’s all. :slight_smile:

What did you try to make it work?: I was using the text field with this snip I found: $str(round(routineTimer.getTime(),0)) but I need to transfer it into minutes.

What specifically went wrong when you tried that?: Nothing in particular.
Include pasted full error message if possible. “That didn’t work” is not enough information.

You’re on the right track. You just need to do some arithmetic on the number in seconds, e.g. this formatted string would get you in the ballpark:

f'{int(routineTimer.getTime() / 60)}:{int(routineTimer.getTime() % 60)}'

i.e. get the number of minutes from dividing by 60 and the number of additional seconds from the modulo operator % (i.e. the number of seconds in the remainder left by dividing by 60).

int() might be better than round()here, as you don’t want to round the number of minutes up - int() will just chop off the whole fractional part.

This would need to be improved to put a leading zero in front of the seconds when it reaches single digits. That would be easier if you do the calculations of minutes and seconds separately in a code component, and then just do the string formatting in the text component, e.g. calculate variables:

# in the "each frame" tab of a code component:
minutes = int(routineTimer.getTime() / 60)
seconds = int(routineTimer.getTime() % 60)

and then put this f-string in the text component:


which will take care of the leading zero for the seconds.

Make sure you put the code component above the text component, so the latter gets to refer to the latest calculated values of the variables.

Thank you very much for your fast response. I’m don’t quite understand the meaning of a “formatted string” i.e. I have never worked with it. Would you mind explaining this to me since I did’t get it to work? :slight_smile:
Remember I’m really at the start of psychopy and have no coding history :smiley:

A formatted string is just like a regular string but has an f in front of the first quotation mark. This tells Python to interpret anything inside its curly brackets as a Python expression rather than as literal text.

There are many ways of formatting Python strings, f-strings are the latest but probably also the easiest to use:

e.g. Without using string formatting, a cruder approach would be:

str(minutes) + ":" + str(seconds)

but then also still need to figure the leading zero issue with the last part.

That doesn’t give us anything to go on - to give useful suggestions, we really need a specific description of the problem, or any error message that occurs.

Huh, it did work now I just forgot to put the $ before the coding. What a bloody mistake. :smiley:
I’m very grateful for your explanation and the links for the formation tutorial, you’re doing me a huge favour. Thanks so much. :smiley: