- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试重新创建 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/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!