我在Python 3中为我的Monty Hall游戏刺激器提供了错误的输出



我正在尝试重现蒙蒂大厅游戏(这是游戏的描述(在哪里要求用户输入一个数字(我的代码要求"输入随机种子"(开始,如果用户未输入数字,则游戏将显示错误消息。如果用户确实输入一个数字,则游戏将启动。然后,游戏将询问用户要测试游戏多少次,因此,如果用户说5,那么游戏将重复五次,一旦用户完成所有五场游戏,它将再次询问用户。但是,如果用户输入字符串/非数字,例如"五",则游戏应要求用户输入一个数字,直到用户最终输入数字为止。

这是输入可以是

的示例
25
5
exit

这是输出应为

Enter Random Seed:
Welcome to Monty Hall Analysis
Enter 'exit' to quit.
How many tests should we run?
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 1
Monty Selects Door 2
Player should stay to win.
Game 3
Doors: ['G', 'C', 'G']
Player Selects Door 2
Monty Selects Door 1
Player should stay to win.
Game 4
Doors: ['C', 'G', 'G']
Player Selects Door 1
Monty Selects Door 2
Player should stay to win.
Game 5
Doors: ['G', 'C', 'G']
Player Selects Door 3
Monty Selects Door 1
Player should switch to win.
Stay Won 60.0% of the time.
Switch Won 40.0% of the time.
How many tests should we run?
Thank you for using this program.

这是我的程序输出

Enter Random Seed:
Welcome to Monty Hall Analysis
Enter 'exit' to quit.
How many tests should we run?
Please enter a number:
How many tests should we run?
Traceback (most recent call last):
  File "monty.py", line 16, in <module>
    tests = int(input('How many tests should we run?n'))
EOFError: EOF when reading a line

这是我的代码

import random
G=0
G2=0
C=0
try:    
    random_seed = input('Enter Random Seed:n')
    random_seed = int(random_seed)
    print('Welcome to Monty Hall Analysis')
    exit = input("Enter 'exit' to quit.n")
    while True:
            if exit == 'exit':
                    print('Thank you for using this program.')
                    break
            try:
                    tests = int(input('How many tests should we run?n'))
                    tests = int(tests)
            except ValueError:
                    print('Please enter a number:')
                    continue
            select_door = ['G', 'C', 'G']
            random.shuffle(select_door)
            player_door = int(input('Doors: '+str(select_door)))
            if random.shuffle(select_door) == "['G', 'C', 'G']":
                    G = 1
                    C = 2
                    G2 = 3
            elif random.shuffle(select_door) == "['C', 'G', 'G']":
                    C = 1
                    G = 2
                    G2 = 3
            elif random.shuffle(select_door) == "['G', 'G', 'C']":
                    G = 1
                    G2 = 2
                    C = 3
            if player_door == C:
                    print('Player should stay to win')
            else:
                    print('Player should switch to win')
            doors = [1 , 2 , 3]
            random.choice(doors)
            print('Player Selects Door: ', player_door)
            print('Monty Selects Door: ', random.choice(doors)) 
except ValueError:
    print('Seed is not a number!')

对不起,如果我的代码真的很糟糕或没有意义。我是一个新的程序员。此外,我只是想让实际的游戏开始工作,然后才能赢得百分比的赢得胜利,而%switch赢得了一部分。

您将此代码放入无限循环中,这就是为什么每次都称为

try:
    tests = int(input('How many tests should we run?n'))
    tests = int(tests)
except ValueError:
    print('Please enter a number:')
    continue

这是我的修复:

import random
G=0
G2=0
C=0
try:    
    random_seed = input('Enter Random Seed:n')
    random_seed = int(random_seed)
    print('Welcome to Monty Hall Analysis')
    exit = input("Enter 'exit' to quit.n")
    while True:
        if exit == 'exit':
                print('Thank you for using this program.')
                break
        try:
                tests = int(input('How many tests should we run?n'))
                tests = int(tests)
        except ValueError:
                print('Please enter a number:')
                continue
        stay_cases = 0
        switch_cases = 0
        for _ in range(tests):
            select_door = ['G', 'C', 'G']
            random.shuffle(select_door)
            player_door = int(input('Doors: '+ str(select_door)))
            if random.shuffle(select_door) == "['G', 'C', 'G']":
                    G = 1
                    C = 2
                    G2 = 3
            elif random.shuffle(select_door) == "['C', 'G', 'G']":
                    C = 1
                    G = 2
                    G2 = 3
            elif random.shuffle(select_door) == "['G', 'G', 'C']":
                    G = 1
                    G2 = 2
                    C = 3
            if player_door == C:
                    print('Player should stay to win')
                    stay_cases += 1
            else:
                    print('Player should switch to win')
                    switch_cases += 1
            doors = [1 , 2 , 3]
            random.choice(doors)
            print('Player Selects Door: ', player_door)
            print('Monty Selects Door: ', random.choice(doors))
        print('Stay Won {}% of the time.'.format((stay_cases/tests) * 100))
        print('Switch Won {}% of the time.'.format((switch_cases/tests) * 100))
except ValueError:
    print('Seed is not a number!')

我添加了for _ in range(tests),主游戏引擎应运行tests次。

我还实施了Stay won ... of the time

的部分

我给您的代码应该做这项工作,但是,如果用户选择不是提供门之一的门,它将崩溃。为避免这种情况,您可以使用if player_door not in doors检查用户选择是否是提供的。

最新更新