我一直在python 3中为我的Monty Hall游戏刺激器输出错误的输出

时间:2018-02-05 16:21:54

标签: python python-3.x

我正在尝试重建Monty Hall游戏(here is a description of the game) 它会要求用户输入一个数字(我的代码要求“输入随机种子”)开始,如果用户没有输入数字,游戏将显示错误消息。如果用户确实输入了数字,则游戏将开始。然后游戏将询问用户他们想要测试游戏的次数,因此如果用户说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!')

很抱歉,如果我的代码非常糟糕或者没有意义。我是一个新的程序员。此外,我只是想让实际的游戏开始工作,然后我就可以继续使用%stay并赢得%switch。

1 个答案:

答案 0 :(得分:1)

你把这段代码放在一个无限循环中,这就是每次调用它的原因

@equals(triggerBody()?['data']['operationName'], 'Microsoft.Compute/virtualMachines/write'

这是我的修复:

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检查用户选择是否是提供的用户之一。