平均胜利

时间:2012-09-27 16:55:28

标签: python

我只是在学习python(有史以来第一语言)并且正在以我觉得有趣的方式实现我所发现的东西。我建了一个伪老虎机赔率计算器。然而,它停止在一个大奖获胜。有没有办法让它一遍又一遍地试图让n个游戏获得大奖的平均次数?

这是我的代码

#!/usr/bin/env python
import random

a = 1

while a >0 :
    l1 = random.randrange(36)
    l2 = random.randrange(36)
    l3 = random.randrange(36)

    print l1, l2, l3
    if l1 == l2 == l3 == 7:
        print 'grand prize winner!!!'
        break
    elif l1 == l2 == l3:
        print 'you won! congratulations'
        print 'it took', a, 'attempts to win'
    else:
        a += 1
        print 'sorry...  try again'
        print 'attempt', a

另外,有没有办法告诉我在赢得大奖的过程中有多少正常的胜利

4 个答案:

答案 0 :(得分:2)

大奖break块中的if语句退出外部while循环。如果您希望继续使用,请移除break。另外,作为一个样式点,while True:while 1:对于创建无限循环的方式更为清晰。至于问题的第二部分,您有a计数器,但您可能希望捕获更多数据,如下所示:

import random

def play(till_jackpot_count):
    game_data_per_jackpot = [{'plays' : 0, 'wins' : 0}]
    wheel_values = xrange(36)
    wheels = [0, 0, 0]
    while till_jackpot_count >= len(game_data_per_jackpot):
        wheels = [random.choice(wheel_values) for wheel in wheels]
        game_data_per_jackpot[-1]['plays'] += 1
        print '%d plays since last jackpot' % game_data_per_jackpot[-1]['plays']
        print '%d wins since last jackpot' % game_data_per_jackpot[-1]['wins']
        print '%d total plays' % sum([data['plays'] for data in game_data_per_jackpot])
        print '%d total wins' % sum([data['wins'] for data in game_data_per_jackpot])
        print '%d total jackpots' % (len(game_data_per_jackpot) - 1)
        print 'this play: {} {} {}'.format(*wheels)
        if len(set(wheels)) == 1:
            if wheels[0] == 7:
                print 'jackpot!'
                game_data_per_jackpot.append({'plays' : 0, 'wins' : 0})
            else:
                print 'win!'
                game_data_per_jackpot[-1]['wins'] += 1
    return game_data_per_jackpot[:-1]

play(10)

我还偷了一个控件till_jackpot_count在顶部,这将使循环结束在那个累积奖金之后。如果你想在函数本身之外进一步分析测试结果,该函数也会返回测试结果,但结果会因为没有分配给任何东西而在这里被删除。

对于您自己的研究,此代码使用列表([]),dicts({}),元组(()),旧式字符串格式('%d' % var),新样式字符串格式('{} {} {}'.format(*iterable)),列表推导([a for a in b]),切片(list[:])和一些内置(sumlen)另外到您已经熟悉的random库和while循环。我还将您的random.randrange()换成了一个更简单,可能更有效,random.sample()的预建xrange()

答案 1 :(得分:0)

你只需要在每次获胜时计算,然后在最后打印总数......(见下面的wins

#!/usr/bin/env python
import random
def play():
  a = 1
  wins = 0
  while a >0 :
    l1 = random.randrange(36)
    l2 = random.randrange(36)
    l3 = random.randrange(36)

    print l1, l2, l3
    if l1 == l2 == l3 == 7:
        #print 'grand prize winner!!!'
        break
    elif l1 == l2 == l3:
        #print 'you won! congratulations'
        #print 'it took', a, 'attempts to win'
        wins += 1
    else:
        a += 1
        print 'sorry...  try again'
        print 'attempt', a
  return a,wins
  #print "You Won %s Times!"%wins

play_again = True
while play_again:
    tries,wins = play()
    print "It Took %d Tries to win the Grand Prize"%tries
    print "You won normally %d Times"%wins
    play_again = raw_input("Play Again(y/n)?")==y

答案 2 :(得分:0)

要运行一些代码n次,您应该写

for _ in range(n):
    # some code

要在某些条件成立时运行它,您应该写

while condition:
    #some code

要无限地运行它,你应该写

while 1:
     # some code

(因为条件1始终为真)。

即使条件不成立,关键字break也会让您离开您所处的循环。


首先设置老虎机代码:

GRAND_PRIZE = 0
REGULAR_PRIZE = 1
NO_PRIZE = 2

def slot_machine():
    l1 = random.randrange(36)
    l2 = random.randrange(36)
    l3 = random.randrange(36)

    if l1 == l2 == l3:
        return GRAND_PRIZE if l1 == 7 else REGULAR_PRIZE
    else:
        return NO_PRIZE

接下来,设置代码以运行此操作直到您获胜。

def run_until_win():
    num_tries = 0
    num_wins = 0

    while 1:
        num_tries += 1
        result = slot_machine()

        if result == GRAND_PRIZE:
            break
        elif result == REGULAR_PRIZE:
            num_wins += 1

    return num_tries, num_wins

最后,运行十次:

for _ in range(10):
    print "tries: {0}, wins: {1}".format(*run_until_win())

答案 3 :(得分:0)

你的'while'循环没有任何问题,它应该逃脱。现在,如果您想多次运行它,可以将其包装在for i in range(number_of_games)循环中。每次退出while循环时,都会将a附加到列表中。

nb_attempts = []
for i in range(nb_games):
    while True:
        ...
        if (l1 == l2 == l3 == 7):
            nb_attempts.append(a)
            break
        elif ...

n个游戏结束时,您会有n次尝试次数列表。尝试次数。