gpt4 book ai didi

python - 我在 python 3 中为我的 Monty Hall 游戏刺激器不断得到错误的输出

转载 作者:行者123 更新时间:2023-11-28 18:15:37 25 4
gpt4 key购买 nike

我正在尝试重新创建 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 won 和 % switch won 部分。

最佳答案

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

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

关于python - 我在 python 3 中为我的 Monty Hall 游戏刺激器不断得到错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48626946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com