| Reference | Downloads | Github

Multiply random numbers between 2 and 11

Hello everybody,
I am currently working on an experiment, where the participant has to multiply two numbers with each other.
The multiplying tasks should be generated randomly. So for each new task there should be two random selections for a number between 2 and 11 and these two numbers make then the multiplying task for the participant.
So for Example 3 *4 , 11 * 10, 5 *5 ,…

I am not sure how to do this. Is it useful to use an excel file for that? If somebody could help me I would be very grateful.

Also the participant should get a feedback afterwords how many % of his responses are currently at the moment. So e.g when the participant answers the first task correctly then the correct responses is 100%, but if he answers the second one incorrect it is only 50% and so on.

I am totally lost how to do this.
Thanks in advance for any help you have!!

Best wishes Sarah

randint(2,12) should give a random integer from 2 to 11. You’ll need str() to turn it into a string for display purposes. For the proportion correct just keep a tally of the number of questions asked and the number of correct answers. I have a template experiment which includes an age question which is set up for numeric responses.

Thanks for your help!
Where can I find you template experiment?

So what I am trying to achieve (for now) is this:

I want to display two numbers (random between 2 and 11) and then a line where the participant can type in his or her answer for the task.

So like this e.g:

What I tried to do now with my code is this:
I added a code component with this code:

Bildschirmfoto 2020-06-21 um 13.14.16

Bildschirmfoto 2020-06-21 um 13.14.26

And then I also added a text component with the text $msg.
But nothing happens. What do I have to do in order to achieve what I need??

Thanks in advance!

You could show the answer they give with the same text component as the question.

Hey @wakecarter,

sorry for my late respond. I was very busy with other things.
Unforunatly I can not open the template you send me in PsychoPy. I don’t know why… if you could just send me the .psyexp file I would be very grateful.

What exactly do you mean by “You could show the answer they give with the same text component as the question.”
So I just need one text component?

I am still struggling with the multiplication itself because it is not working… If anyone could help me with that I would be very thankful!

Thanks in advance,

I’ve attached the psyexp file for my template.

template.psyexp (81.2 KB)

What is your current multiplication code and what error is it giving?

Thanks a lot! Now I can open it and I will have a look at it.

So here is my current experiment:

And my multiplication code looks like this:
Bildschirmfoto 2020-07-29 um 14.49.01

Bildschirmfoto 2020-07-29 um 14.49.08

And the error I am getting is: msg = str(leftNumber) * str(rightNumber)
TypeError: can’t multiply sequence by non-int of type ‘str’

So I have to delete the str but then I don’t know how I should display it as a text.

Here two images so you know how I want it to be:
First the task (where the user can then input the answer)

And then after every task this should be displayed:

Here is also my .psyexp file in case my description was too complicated or not understandable.
rechnen.psyexp (10.2 KB)

Thank you so much!!

I think you want msg = str(leftNumber)+’ x ‘+str(rightNumber)+’ = '+response+cursor

It could then be set every frame (assuming this is local not online)

Yes I think that is what I want. Thank you.
And then I just define a new variable corrAns and save in this variable leftNumber * rightNumber or how do I tell psychopy what the correct answer is?

I tried the code in PsychoPy (without the + response and + cursor, I just want to see first if it displays anything) and turns out that I can’t see anything and I also getan ApplepersistanceignoreState Error:

Bildschirmfoto 2020-07-29 um 15.18.48

Do you have any idea why I get this error or why I can’t see anything??

Make sure your quotes are simple, not smart.

Please show a screenshot of your code component so I can be sure of what you’re trying.

Sure! Sorry!

Bildschirmfoto 2020-07-29 um 15.43.53

Bildschirmfoto 2020-07-29 um 15.44.13 Bildschirmfoto 2020-07-29 um 15.44.24

And then I have a TextComponent and in the field Text I put in $msg.

Move the code component above the text component so msg gets set before the text is displayed.

However, that would explain why it’s blank, not why it’s crashing.

Ahh that makes sense. Perfect, thank you. Now that works.
And I also figured out how to get the UserInput.

But now I am struggling with how to tell psychoPy what the correct Answer is.

I created a new variable corrAns and assigned the value of rightNumber * leftNumber to it.
But now I don’t now how I can compare this with the UserInput.

This is how my code Component looks right now:
Bildschirmfoto 2020-07-29 um 19.26.27

Bildschirmfoto 2020-07-29 um 19.27.22

In the each frame panel I wanted to to a if query with corrAns. But I don’t now how to get the number the user typed in out of my code.
Do I have to do it differently with the UserInput for my case?

Thanks for you help!!!

Why do you want to check for the correct answer every frame? Don’t you want the participant to choose to submit their answer first?

In End Routine you may need to use int(userInput) so you can compare it with correct answer

Okay thats true I only need to do it in end routine. I don’t know why I wanted to do it in each frame in the first place.

Where I am still struggling is how to access the int(userInput). My Code in end routine looks like this now (the rest hasn’t really changed):
Bildschirmfoto 2020-07-29 um 22.04.24

The code with answers = ‘100%’ is not the final code I just typed it in to see, if it works to access the user input.

Obviously PsychoPy gives me the error userInput is not defined, because I have never defined it.
Sorry I know this is a very basic question but I have googled quite a while now and I still can’t see how I can access the userInput with corrAns. I would be very thankful if you could help me with this issue.

In your case the user input is text.text (unless you can’t read that as a value online)

1 Like

Thank you so much.
I thought so first but then I then I got confused somehow.

Okay I have one (hopefully) last question and then I won’t disturb you any more.
Is it possible that my code component of my trial routine communicates with my tracker routine?

Bildschirmfoto 2020-07-29 um 22.32.20

Because after the participant answered the task I want to show how much percent of the questions he got right, how many trials he has already done and how much time it has taken him. (I think I posted a screenshot of how this should look earlier here). And for that I need to access my variables of the code component of the trial routine.

Thank you for all your time and help!!

Or wait I think I know the answer is yes. They can communicate.
Then my last question would be how I do the percent calculation.

E.g. If the participant answers the first one correctly it should display 100% as correct responses. If the participant answers the second one also correctly it is still 100%. But if the participant answers the third one incorrectly it should display 66.67 % as correct responses.

Do you have any idea how to achieve this??

If you have one variable, score, which you increment on each correct answer, and another variable, trials, which increments on each trial, then the percentage correct (for display) would be


if scorePercent==round(scorePercent):
1 Like

You’re a genius!! Thanks! That makes sense!

I thought I’ve understood how I can communicate between code components and routines but maybe I was wrong because it is not properly working.

Can my corrAns variable from the Code component of my trial routine…:

…communicate with my code component in my tracker routine?:

Because if I type in the correct Answer the feedback message that is displayed is still “Falsch!”. Am I completely lost here or is it just a small mistake I made here?

I am also not quite sure, why it is necessary that I type something in the End Routine in the code component of trial routine because I don’t think I need it for my purpose.