简体   繁体   English

让我们做个交易python游戏

[英]Let's make a deal python game

I am required to code a python program that's based on the old TV show, Let's make a deal.我需要编写一个基于旧电视节目的python程序,让我们做个交易。 I got the program to print out the number of games and if the user should have switched or stayed.我让程序打印出游戏数量以及用户是否应该切换或留下。 Now I am trying to figure out how to print the percentage of the times the user should have stayed and switched altogether.现在我想弄清楚如何打印用户应该停留和完全切换的时间百分比。

here is what the test input is:这是测试输入:

25
7
exit

Heres what the program should output:下面是程序应该输出的内容:

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.

Here is what my program outputs:这是我的程序输出的内容:

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?

Here is the code I made:这是我制作的代码:

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.')

Sorry if my code does not look right or is confusing.抱歉,如果我的代码看起来不正确或令人困惑。 This is my first time programming thanks.这是我第一次编程谢谢。

Well, you keep track of the number of times the player should have stayed to won.好吧,您会跟踪玩家应该留下来赢得胜利的次数。 So the percentage to stay is just "(pStay / float(testNum)) * 100)" then simply subtract that number from 100 to get the percentage to change (since they have to add up to 100%)所以保持的百分比只是“(pStay / float(testNum))* 100)”然后简单地从100中减去该数字以获得改变的百分比(因为它们必须加起来为100%)

Thought I should provide some more info.以为我应该提供更多信息。 The formula is taking the number of stay games out of the total number of games.该公式是从总比赛次数中减去逗留比赛的次数。 You multiply by 100 to convert from a decimal value to percent.乘以 100 可将十进制值转换为百分比。

So if you should have stayed in 1 game, and you played 10 games, it would be 1/10, which is .1, times 100, is 10%.因此,如果您应该留在 1 场比赛中,并且您打了 10 场比赛,那将是 1/10,即 0.1,乘以 100,是 10%。

Since 1/10 you should have stayed, that means that 9/10 you should have changed.因为 1/10 你应该留下,这意味着 9/10 你应该改变。 So you can subtract the stay percentage to get the change percentage, ie 100% - 10% = 90%所以你可以减去停留百分比得到变化百分比,即100% - 10% = 90%

The reason I put the float() conversion in the code is because in python2 if you divide an integer by an integer, it doesn't calculate the fractional part.我将 float() 转换放在代码中的原因是因为在 python2 中,如果将整数除以整数,它不会计算小数部分。 It just rounds down to the integer value.它只是向下舍入到整数值。 So 1/10 would give you 0, not .1.所以 1/10 会给你 0,而不是 0.1。 In python3 it does actually produce a fractional value, but since I don't know which version you are using, it is safe to cast it to a float to get the expected result在 python3 中,它确实产生了一个小数值,但由于我不知道您使用的是哪个版本,因此将其强制转换为浮点数以获得预期结果是安全的

See the added comments below, you were close.请参阅下面添加的评论,您已经接近了。 However you were missing a sum count variable for pSwitch.但是,您缺少 pSwitch 的总和计数变量。 Hope this helps.希望这可以帮助。

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:

    # Total Number of Games
    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
    pSwitch = 0  # Also need a running count var for switch
    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  - - Not sure why you have two increments for pStay.. only need one.
            print('Player should stay to win.')
            pStay += 1
        if var == 1:
            print('Player should switch to win.')
            pSwitch += 1 # Also increment the pSwitch

    # Print out the percentages
    print('\n')    
    print("Percentage of times player should have STAYED: ",(pStay/testNum) * 100, "%")
    print("Percentage of times player should have SWITCHED: ",(pSwitch/testNum) * 100, "%")

Here is a Python 3.6 simulation of a generalized version of Make a Deal using sets.这是使用集合的 Make a Deal 通用版本的 Python 3.6 模拟。 In the generalized version of Make a Deal the number of doors and doors to open can be varied.在 Make a Deal 的通用版本中,门的数量和要打开的门的数量是可变的。 See for reference:见参考:

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

If run with doors = 3 and doors_to_open = 1, the expected result is 33% if the option is not taken to switch doors and 66% when it is.如果在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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM