改进python中的函数

时间:2013-05-02 01:41:12

标签: function python-2.7

我创建了这个函数,以确定投注的任何一方是否投注在特定篮球比赛中将有多少3个指针是有利可图的。我预测在代码中前面会有多少三个指针 pjTotal3s 和标准差 pjGame3STD threes_over 是投注网站给我的数字,如果三个投注的总数超过或低于该数字,我会尝试查找该数字。在这种情况下,它是14.5。

threes_over = 14.5

def overunder(n):
over_count = 0
under_count = 0
push_count = 0
overodds = 0
underodds = 0
for i in range(n):
    if round(np.random.normal(pjTotal3s,pjGame3STD)) > threes_over:
        over_count = over_count + 1
    if round(np.random.normal(pjTotal3s,pjGame3STD)) < threes_over:
        under_count = under_count +1
    if round(np.random.normal(pjTotal3s,pjGame3STD)) == threes_over:
        push_count = push_count + 1
return over_count, under_count, push_count   

然后我将它模拟为100,000 overunder(100000)次,它给出了三个指针的数量将超过,低于或等于给定数量的次数。这工作正常,但我还有更多工作要做,以确定它是否是一个有利可图的赌注。

假设这个输出(57550,42646,0)我必须像这样手动输入它并做更多的事情来确定赌注的任何一方是否值得。

over_count = 57550
under_count = 42646
over = 1/(over_count / (over_count + under_count))
under = 1/ (under_count / (over_count + under_count))
over_odds_given = 1.77
under_odds_given = 2.05
overedge = 1/over * over_odds_given - 1
underedge = 1/under * under_odds_given - 1
print overedge, underedge

如何将第二组计算结合到与第一组相同的功能中。我想避免手动输入第一个函数的结果,以节省时间并避免输入错误的数字。

1 个答案:

答案 0 :(得分:0)

如果您真的想要第二位代码与第一位代码相同,那么您可以将除了设置over_count和under_count的部分之外的所有代码粘贴到函数中...

def overunder(n):
  over_count = 0
  under_count = 0
  push_count = 0
  overodds = 0
  underodds = 0
  for i in range(n):
    if round(np.random.normal(pjTotal3s,pjGame3STD)) > threes_over:
      over_count = over_count + 1
    if round(np.random.normal(pjTotal3s,pjGame3STD)) < threes_over:
      under_count = under_count +1
    if round(np.random.normal(pjTotal3s,pjGame3STD)) == threes_over:
      push_count = push_count + 1

  over = 1/(over_count / float(over_count + under_count))
  under = 1/ (under_count / float(over_count + under_count))
  over_odds_given = 1.77
  under_odds_given = 2.05
  overedge = 1/over * over_odds_given - 1
  underedge = 1/under * under_odds_given - 1

  print overedge, underedge

  return over_count, under_count, push_count   

或者,或许更好,您可以将第二位代码(overedge,underedge)放入一个单独的函数中并将结果传递给overunder:

def edges(over_count, under_count, push_count):
  over = 1/(over_count / float(over_count + under_count))
  under = 1/ (under_count / float(over_count + under_count))
  over_odds_given = 1.77
  under_odds_given = 2.05
  overedge = 1/over * over_odds_given - 1
  underedge = 1/under * under_odds_given - 1
  print overedge, underedge

然后使用overunder的结果调用它:

c = overunder(100000)
edges(c[0],c[1],c[2])