如何在python中创建一个时环,并从我的列表中显示一个随机对象?
因此,我有以下任务:
首先生成一个带有图像文件的虚拟名称字符串的列表(egmy_list ['image1.png','image2.png','image3.png'])。然后编写一段循环,其中图像是从列表中随机绘制的,并呈现直到按下数字1或2。不用显示实际图像,只需使用打印命令(例如print('image1.png') - 您将学习如何通过Psychopy稍后通过Psychopy呈现图像)。首先将受试者的响应定义为布尔值(响应= false)。而不是通过键盘或鼠标注册响应,而是将库随机使用函数randint使用来生成随机响应。最后,如果尚未注册响应,则在最多20个重复后,添加流产信号以中断循环。此外,应根据其演示文稿将随机绘制的“图像”的名称缓存,最后覆盖到文本文件(.csv或.tsv等)中,以便保留它们绘制的顺序。
我已经完成的编码是以下内容,但是如果我尝试启动该操作,什么也不会发生。没有结果。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的if语句
将始终为真。因此,您的回应将永远是正确的。
另外,当打印时,您不想使用Random.Choice Choice(),因为您已经以这种方式导入了它。
your if statement
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.
当您做
1或2
时,它始终是true
,因此您永远不会打印。您应该将变量与它们进行比较,含义:
,如果(1,2)
或var == 1或var == 2
When you do
1 or 2
it's alwaysTrue
so you never get to print.You should compare a variable to them instead, meaning:
if var in (1,2)
orif var == 1 or var == 2