-1

So I have the following task:

First generate a list with strings of fictitious names for image files (e.g.my_list['image1.png','image2.png','image3.png']). Then write a while loop in which an image is randomly drawn from the list and presented until the number 1 or 2 is pressed. Instead of presenting an actual image, just use the print command (e.g. print ('image1.png') - you'll learn how to present images via PsychoPy later). First define the subject's response as a boolean (response = False). Instead of registering a response via keyboard or mouse, use the library random with its function randint to generate a random response. Finally, add an abort signal to interrupt the loop after a maximum of 20 repetitions if no response has been registered yet. Additionally, the names of the randomly drawn "images" should be cached according to their presentation and finally overwritten into a text file (.csv, or .tsv etc.) so that the order in which they were drawn is preserved.

The coding I have already done is the following, but if I try to start the operation nothing happens. There is no result.

from random import randint
from random import choice
 
imagefiles = ["image1.png","image2.png","image3.png"]
if 1 or 2:
    response = True
else:
    response = False
tries = 1
while tries <= 20:
    if response == False:
        print(random.choice(imagefiles))
        break 
    tries = tries + 1
H L
  • 1
  • `response = randint(1,10) in (1,2)` might be helpfull - if you put that into the loop you get a "random" value that is True or False - now you just need a counter to break from your loop if you get 20 times some random value > 2. – Patrick Artner Apr 29 '22 at 11:13

2 Answers2

0

your if statement

if 1 or 2:

will always be true. So your response will always be true. Also when printing, you don't want to use random.choice only choice(), as you've already imported it this way.

MegaKarg
  • 110
  • 1
  • 7
0

When you do 1 or 2 it's always True so you never get to print.

You should compare a variable to them instead, meaning: if var in (1,2) or if var == 1 or var == 2

moshevi
  • 4,999
  • 5
  • 33
  • 50