如何编写一个基于条件加倍int的循环

时间:2014-03-14 19:55:30

标签: python

100,000 satoshies = 0.001 btc

somethingRandom(btc):
       #code

btc = 0.001
satoshies = btc / 0.00000001


if somethingRandom():
         #double 
         somethingRandom(btc)
else:
         #reset
          somethingRandom(btc)

如果somethingRandom返回true,我想通过将btc的数量增加一倍来继续增加satoshies的数量

如果返回false,我想将btc重置为0.001

有什么想法吗? 感谢。

3 个答案:

答案 0 :(得分:0)

喜欢这个吗?

def somethingRandom():
  return random.choice((True, False))

btc = 0.001

while True:
  if somethingRandom():
    btc *= 2
  else:
    btc = 0.001
    satoshies = btc / 0.00000001

答案 1 :(得分:0)

以下是我将如何实现随机二项(True或False)函数:

def binomial(p=.5):
    '''return True with probability p, else False with probability 1-p'''
    return random.random() < p

这就是我要做的就是实现你想要的东西:

btc = 0.001
def satoshies():  # needs to be in a function, because recalculating based on btc change:
    return btc / 0.00000001


if somethingRandom():
    btc *= 2
else:
    btc = .001

satoshies()

答案 2 :(得分:0)

如果您喜欢使用itertools

import itertools

btc *= 2**(sum(itertools.takewhile(lambda x: somethingRandom(), itertools.repeat(1))))