python 中的废话
我正在尝试模拟 n 场双骰子游戏。该代码对我来说似乎很有意义,但我从未得到正确的结果。例如,如果我输入 n = 5,即五场比赛,则胜利和失败的总和大于 5。
它的工作原理如下:如果初始掷骰子数为 2、3 或 12,则玩家失败。如果掷骰结果为 7 或 11,则玩家获胜。任何其他初始掷骰都会导致玩家再次掷骰。他继续掷骰,直到掷出 7 或初始掷骰的值。如果他在掷出 7 之前重新掷出初始值,则获胜。首先掷出 7 就输了。
from random import randrange
def roll():
dice = randrange(1,7) + randrange (1,7)
return dice
def sim_games(n):
wins = losses = 0
for i in range(n):
if game():
wins = wins + 1
if not game():
losses = losses + 1
return wins, losses
#simulate one game
def game():
dice = roll()
if dice == 2 or dice == 3 or dice == 12:
return False
elif dice == 7 or dice == 11:
return True
else:
dice1 = roll()
while dice1 != 7 or dice1 != dice:
if dice1 == 7:
return False
elif dice1 == dice:
return True
else:
dice1 = roll()
def main():
n = eval(input("How many games of craps would you like to play? "))
w, l = sim_games(n)
print("wins:", w,"losses:", l)
I'm trying to simulate n games of craps. The code seems to make sense to me but I never get the right result. For example, if I put in n = 5 i.e. fives games the wins and losses sum to something greater than 5.
Here's how it's supposed to work: if initial roll is 2, 3, or 12, the player loses. If the roll is 7 or 11, the player wins. Any other initial roll causes the player to roll again. He keeps rolling until either he rolls a 7 or the value of the initial roll. If he re-rolls the initial value before rolling a 7, it's a win. Rolling a 7 first is a loss.
from random import randrange
def roll():
dice = randrange(1,7) + randrange (1,7)
return dice
def sim_games(n):
wins = losses = 0
for i in range(n):
if game():
wins = wins + 1
if not game():
losses = losses + 1
return wins, losses
#simulate one game
def game():
dice = roll()
if dice == 2 or dice == 3 or dice == 12:
return False
elif dice == 7 or dice == 11:
return True
else:
dice1 = roll()
while dice1 != 7 or dice1 != dice:
if dice1 == 7:
return False
elif dice1 == dice:
return True
else:
dice1 = roll()
def main():
n = eval(input("How many games of craps would you like to play? "))
w, l = sim_games(n)
print("wins:", w,"losses:", l)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题在于 相反
,它应该
在您的代码中,您正在模拟两个游戏而不是一个(通过调用
game()
两次)。这给出了四种可能的结果,而不是两种(赢/输),从而给出了不一致的总体结果。The problem is with
Instead, it should be
In your code, you are simulating two games instead of one (by calling
game()
twice). This gives four possible outcomes instead of two (win/loss), giving inconsistent overall results.在此代码中,
您调用了
game()
两次,因此您在那里玩了两个游戏。您想要的是 else 块:顺便说一句,您可以使用
in
简化逻辑:代码实际上就是您给出的描述。
In this code
you call
game()
twice, so you play two games right there. What you want is a else block:Btw, you can simplify the logic with
in
:The code is quite literally the description you gave.
不要这样做,
效果根本不好。
Don't do this
It doesn't work out well at all.
这段代码有很多问题。最重要的是,每个循环都会调用 game() 两次。您需要调用它一次并存储结果,然后基于该结果进行切换。
There are numerous problems with this code. Most importantly, you're calling game() twice per loop. You need to call it once and store the result, and switch based on that.
面向对象的重写:
An OO rewrite: