[英]Python - How to end routine by probability which updates itself with every 'iteration'?
So I've been working on an adaptation of the Balloon Analogous Risk Task.所以我一直在对气球类比风险任务进行改编。 The gist of it is that the participant gets 30 trials (3 condition, 10 trials per condition) and in each of these trials they can fill a balloon with air by pressing the space bar as often as they want.
其要点是参与者进行 30 次试验(3 个条件,每个条件 10 次试验),并且在每个试验中,他们可以按他们想要的次数按空格键将气球充满空气。 The larger the balloon, the larger the amount of money they can get.
气球越大,他们能得到的钱就越多。 They can cash out at any time, even without filling the balloon with air once (then they receive $0 for this trial).
他们可以随时兑现,即使没有一次用空气填充气球(然后他们在此试用中收到 0 美元)。 However, the balloon can also pop.
但是,气球也可以弹出。
I would like to design the 3 conditions as follows: In the first condition, balloons have an intial value of 1% to pop when clicking the space bar.我想设计以下 3 个条件:在第一个条件下,气球的初始值为 1%,单击空格键时会弹出。 In the second and third condition, it should be 2% and 5%, respectively.
在第二和第三个条件下,应该分别为 2% 和 5%。 These probabilities of a balloon popping should apply at the very first moment one presses the space bar and increase by the base rate after every time the space bar is pressed.
这些气球爆裂的概率应该在一个人按下空格键的第一刻应用,并在每次按下空格键后以基本速率增加。 So for condition 1, it would be 1% before pressing space bar the first time, after that 2%, 3%, etc. The other conditions should be 2%, 4%, 6%, etc. and 5%, 10%, 15%, etc. As I said, these conditional values are already stored in a excel-file I read in with my script.
所以对于条件1,第一次按空格键之前是1%,之后是2%、3%等。其他条件应该是2%、4%、6%等和5%、10% , 15% 等。正如我所说,这些条件值已经存储在我用脚本读入的 excel 文件中。
Do you have an idea how to program this in Python?你知道如何在 Python 中编程吗? I am new to Python and my first attempt was directed at the sum of times someone pressed the space bar.
我是 Python 的新手,我的第一次尝试是针对有人按下空格键的总和。 It also works, but I would rather have probabilities.
它也有效,但我宁愿有概率。
I changed my description due to a very helpful comment below by @shmulvad.由于@shmulvad 下面的非常有用的评论,我更改了我的描述。 It was not clear what I wanted.
不清楚我想要什么。 I hope it's now easier to understand.
我希望现在更容易理解。 Thank you in advance!
先感谢您!
To test something with a certain probablity, you can in general do:要以一定的概率测试某些东西,您通常可以执行以下操作:
import random
rnd_val = random.random() # Generates a pseudorandom value between 0 and 1
prob = 0.05
print(rnd_val < prob) # Prints True 5% of times, otherwise False
Knowing this, we can accomplish your goal the following way:知道了这一点,我们可以通过以下方式实现您的目标:
MONEY_PER_PUMP = 2
MAX_PUMPS_PER_TRIAL = 10
BASE_RISKS = [0.01, 0.02, 0.05]
total_money = 0
for base_risk in BASE_RISKS:
n_pumps, risk_of_popping = 0, 0
while True:
is_done_with_trial = n_pumps >= MAX_PUMPS_PER_TRIAL
if not is_done_with_trial and event.getKeys(['space']):
n_pumps += 1
risk_of_popping += base_risk
if random.random() < risk_of_popping:
break
elif is_done_with_trial or user_wants_to_exit_early:
total_money += MONEY_PER_PUMP * n_pumps
break
After the loop exits, total_money
will contain the amount of money the person playing have won if I have understood you correctly.循环退出后,如果我理解正确,
total_money
将包含玩家赢得的金额。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.