Can't display total points (though no issue with counting & storing)

OS: Win11
PsychoPy version: 2023.2.3
Standard Standalone Installation? y
Do you want it to also run online? n
What are you trying to achieve?:

I would really appreciate any help with this!

Currently my task is working great. However, I am struggling with displaying the total number of credits at the end of the task. It counts and stores them correctly in the CSV. But, for some reason, I cannot display it at the end as a text component (no matter what I try).

Begin experiment:

credit = 0

Total_credits = 0

End routine: (To clarify, the Gamefeedback is in another text component, and that works perfectly!)

if key_resp_28.keys == ‘y’:
credit += Comb_list[0]
GameFeedback = ‘Well done! You answered the question correctly and won ’ + str(Comb_list[0]) + ’ credit(s)!’
winnings = ‘Reward’ + str(Comb_list[0]) + ‘.png’
thisExp.addData(‘Total_credits’, credit)
else:
GameFeedback = ‘Sadly, your answer was wrong. Better luck next time!’
winnings = ‘Nothing1.png’

On the next routine on Game3 (before the text is displayed - on end routine):

msg = “You have finished the task. You won " + str(‘Total_credits’) + " credits. Press space to end”

msg is in the text component.

It is saved in the csv as Total_credits

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

20

24

25

31

41

42

52

53

And I want to display that last column since that is the total at the end.

What did you try to make it work?:

I have tried:

  • $Total_credits (only) in the text component.
  • Total_credits instead of str().
  • str(credit)) when saving to add Data
  • Initialising the variable with = 0, [], and not at all.
  • removing the str in the msg message so simply + ‘Total_credits’
  • Adding msg text in a different routine (e.g., beginning the routine with it instead of before, and initialising it at the beginning).

Link to the most relevant existing thread you have found:

Display points earned in a decision task for both per-trial points and cumulative points - Builder - PsychoPy

What specifically went wrong when you tried that?

Displays either:

  • 0
  • Total_credits
  • msg
  • [ ]

Forgot to add that this is what it looks like.

The end routine is where I am displaying the text.

Hello @Hans

The main issue is in your msg line:

msg = "You have finished the task. You won " + str('Total_credits') + " credits. Press space to end"

Because 'Total_credits' is in quotes, Python treats it as a literal text string rather than your variable (which is named credit, and is only saved under the column label 'Total_credits' via thisExp.addData('Total_credits', credit)).

That also explains the outputs you saw:

  • Total_credits: str('Total_credits') literally returns the word Total_credits.

  • 0: Total_credits was initialized to 0 at the start of the experiment and never updated.

  • msg: Putting msg in a Text component without a leading $ displays the literal word msg instead of the variable’s content.

  • []: A variable was initialized or overwritten as an empty list [] somewhere and printed directly.

How could you fix this.

  1. Keep updating credit in your trial routine and save it with thisExp.addData('Total_credits', credit).

  2. Build msg using credit (unquoted), e.g. in End Routine of the preceding routine:

  msg = "You have finished the task. You won " + str(credit) + " credits. Press space to end"
  1. In your final Text component, set the text field to $msg (with set every repeat).

  2. Replace curly quotes (‘ ’ “ ”) with straight quotes (' ") .

Best wishes Jens

Thank you so much! This worked amazingly! I can’t believe I didn’t think of this.