让我们进行一场交易蟒蛇游戏

时间:2018-02-10 00:44:57

标签: python logic

我需要编写一个基于旧电视节目的python程序,让我们做一笔交易。我得到程序打印出游戏的数量,如果用户应该切换或停留。现在我想弄清楚如何打印用户应该留下并完全切换的次数百分比。

以下是测试输入:

25
7
exit

这是程序应该输出的内容:

Game 1
Doors : [ ’G’, ’C’, ’G’ ]
Player Selects Door 1
Monty Selects Door 3
Player should switch to win.
Game 2
Doors : [ ’C’, ’G’, ’G’ ]
Player Selects Door 2
Monty Selects Door 3
Player should switch to win.
Game 3
Doors : [ ’G’, ’C’, ’G’ ]
Player Selects Door 1
Monty Selects Door 3
Player should switch to win.
Game 4
Doors : [ ’C’, ’G’, ’G’ ]
Player Selects Door 2
Monty Selects Door 3
Player should switch to win.
Game 5
Doors : [ ’G’, ’G’, ’C’ ]
Player Selects Door 3
Monty Selects Door 1
Player should stay to win.
Game 6
Doors : [ ’G’, ’C’, ’G’ ]
Player Selects Door 2
Monty Selects Door 1
Player should stay to win.
Game 7
Doors : [ ’G’, ’G’, ’C’ ]
Player Selects Door 2
Monty Selects Door 1
Player should switch to win.
Stay Won 28.6% of the time.
Switch Won 71.4% of the time.
How many tests should we run?
Thank you for using this program.

这是我的程序输出的内容:

Enter Random Seed:
25
Welcome to Monty Hall Analysis
Enter 'exit' to quit
How many tests should we run?
7
Game 1
Doors: ['G', 'C', 'G']
Player Selects Door 1
Monty Selects Door 3
Player should switch to win.
Game 2
Doors: ['G', 'C', 'G']
Player Selects Door 2
Monty Selects Door 1
Player should stay to win.
Game 3
Doors: ['C', 'G', 'G']
Player Selects Door 1
Monty Selects Door 3
Player should stay to win.
Game 4
Doors: ['G', 'G', 'C']
Player Selects Door 3
Monty Selects Door 2
Player should stay to win.
Game 5
Doors: ['G', 'G', 'C']
Player Selects Door 3
Monty Selects Door 2
Player should stay to win.
Game 6
Doors: ['G', 'C', 'G']
Player Selects Door 3
Monty Selects Door 1
Player should switch to win.
Game 7
Doors: ['C', 'G', 'G']
Player Selects Door 2
Monty Selects Door 3
Player should switch to win.
How many tests should we run?

这是我制作的代码:

import random
import sys

try:
    randSeed = int(input('Enter Random Seed:\n'))
    random.seed(randSeed)
except ValueError:
    sys.exit("Seed is not a number!")

print('Welcome to Monty Hall Analysis')
print("Enter 'exit' to quit")

while True:
    testNum = input('How many tests should we run?\n')
    valid = False
    while not valid:
        try:
            if testNum == "exit":
                sys.exit("Thank you for using this program.")
            else:
                testNum = int(testNum)
                valid = True
        except ValueError:
            testNum = input('Please enter a number:\n')
    pStay = 0
    pChange = 0
    numGame = 0
    for numGame in range(1, testNum + 1):
        doorList = ['C', 'G', 'G']
        random.shuffle(doorList)
        print('Game', numGame)
        print('Doors:', doorList)
        playerDoor = random.randint(0,2)
        montyDoor = random.randint(0,2)
        print('Player Selects Door', playerDoor+1)
        while montyDoor == playerDoor or doorList[montyDoor] == 'C':
            montyDoor = random.randint(0,2)
        print('Monty Selects Door', montyDoor+1)
        if doorList[playerDoor] == 'C':
            var = 0
        else:
            var = 1

        if var == 0:
            pStay += 1
            print('Player should stay to win.')
            pStay += 1
        if var == 1:
            print('Player should switch to win.')

很抱歉,如果我的代码看起来不正确或令人困惑。这是我第一次编程谢谢。

3 个答案:

答案 0 :(得分:0)

好吧,你要记录玩家应该留下的次数。因此留下的百分比只是“(pStay / float(testNum))* 100)”然后简单地从100减去该数字以获得改变的百分比(因为它们必须加起来为100%)

我想我应该提供更多信息。公式是将游戏数量从游戏总数中扣除。您将乘以100以将十进制值转换为百分比。

所以,如果你应该留在1场比赛中,并且你打了10场比赛,它将是1/10,即.1,倍数100,是10%。

自1/10你应该留下来,这意味着你应该改变9/10。因此,您可以减去停留百分比以获得更改百分比,即100% - 10%= 90%

我在代码中放置float()转换的原因是因为在python2中,如果将整数除以整数,则不会计算小数部分。它只是向下舍入到整数值。所以1/10会给你0,而不是.1。在python3中它实际上产生了一个小数值,但由于我不知道你使用的是哪个版本,所以将它转换为浮点数以获得预期结果是安全的

答案 1 :(得分:0)

看到下面添加的评论,你很接近。但是,您错过了pSwitch的总计数变量。希望这可以帮助。

ATTR_KEEP_ROWS_TOGETHER

答案 2 :(得分:-1)

这是使用集的通用版Make a Deal的Python 3.6模拟。在“达成交易”的通用版本中,可以打开的门和门的数量可以变化。请参阅以供参考:

https://math.stackexchange.com/questions/608957/monty-hall-problem-extended

如果在doors = 3且doors_to_open = 1的情况下运行,则如果未选择切换门的选项,则预期结果为33%,当选择门时为66%。

#!/usr/bin/env python
'''  application of Make a deal statistics
     application is using sets {}
     for reference see:
https://math.stackexchange.com/questions/608957/monty-hall-problem-extended
'''
import random


def Make_a_Deal(doors, doors_to_open):
    '''  Generalised function of Make_a_Deal. Logic should be self explanatory
         Returns win_1 for the option when no change is made in the choice of
         door and win_2 when the option to change is taken.
    '''
    win_1, win_2 = False, False

    doors = set(range(1, doors+1))
    price = set(random.sample(doors, 1))
    choice1 = set(random.sample(doors, 1))
    open = set(random.sample(doors.difference(price).
               difference(choice1), doors_to_open))
    choice2 = set(random.sample(doors.difference(open).
                  difference(choice1), 1))
    win_1 = choice1.issubset(price)
    win_2 = choice2.issubset(price)

    return win_1, win_2


def main():
    '''  input:
         - throws: number of times to Make_a_Deal (must be > 0)
         - doors: number of doors to choose from (must be > 2)
         - doors_to_open: number of doors to be opened before giving the 
           option to change the initial choice (must be > 0 and <= doors-2)
    '''

    try:
        throws = int(input('how many throws: '))
        doors = int(input('how many doors: '))
        doors_to_open = int(input('how many doors to open: '))
        if (throws < 1) or (doors < 3) or \
                (doors_to_open > doors-2) or (doors_to_open < 1):
            print('invalid input')
            return

    except Exception as e:
        print('invalid input: ', e)
        return

    number_of_wins_1, number_of_wins_2, counter = 0, 0, 0

    while counter < throws:
        win_1, win_2 = Make_a_Deal(doors, doors_to_open)

        if win_1:
            number_of_wins_1 += 1
        if win_2:
            number_of_wins_2 += 1

        counter += 1
        print('completion is {:.2f}%'.
              format(100*counter/throws), end='\r')

    print('number of wins option 1 is {:.2f}%: '.
          format(100*number_of_wins_1/counter))
    print('number of wins option 2 is {:.2f}%: '.
          format(100*number_of_wins_2/counter))


if __name__ == '__main__':
    main()