在 Python 中掷 2 个骰子,如果它们的数字相同,则再次掷骰子,然后继续
Rolling 2 dice in Python and if they are the same number, roll again, and continuing
所以我需要编写一个 Python 程序,我需要掷 2 个骰子并打印 2 个骰子的总和。到目前为止我得到了这个:
import random
def monopoly():
x = random.randrange(1,7)
y = random.randrange(1,7)
while True:
if x != y:
print(x, '+', y, '=', x+y)
break
现在,每次 2 个骰子的数字相同时(2 + 2 或 3 + 3 等),您可以再次掷出。如果连续 3 次骰子都相同,则需要进监狱。我以为我必须像这样使用 continue 来处理 while 循环:
else:
if x == y:
print(x + y)
continue
#continuation of the code above
现在,如果我确实得到了骰子相同的结果,它会一遍又一遍地打印出总和,直到我自己停止程序。但是不知道为什么。
我该如何解决这个问题?因为我不知道该怎么做。
您需要在 each 循环迭代中使用新的随机数:
while True:
x = random.randrange(1,7)
y = random.randrange(1,7)
if x != y:
print(x, '+', y, '=', x+y)
break
否则,x
和 y
永远不会改变,所以你的破坏条件永远不会成立。
程序继续循环的原因是因为它处于 while
循环中。
因为它总是True
,所以没有办法打破循环。
起初这可能很奇怪,但是当你看的时候,你会看到 x
和 y
是在循环外定义的,它们总是相同的。
因此,在它们相同的情况下,它总是相同的。
您必须在 else
部分或 while
语句的开头将 x
和 y
重新定义为不同的变量,以便生成新的这两个变量的值,否则每次都给出相同的值。
这是一个结构,您可以使用它来改变玩家的轮次,在两次掷骰之间,然后将玩家送进监狱以掷出 3 个双打。对于双打,我们可以使用 运行 计数,如果它命中 3 将 print('Go to jail')
。这是一个总体思路,供您使用
from random import choice
from itertools import cycle
die = [1, 2, 3, 4, 5, 6]
doubles = 0
players = cycle(['player1', 'player2'])
turn = iter(players)
player = next(turn)
while True:
x, y = choice(die), choice(die)
if x == y:
print(f'{player} folled {x + y}, Doubles!')
print(f'It is {player}\'s turn\n')
doubles += 1
else:
doubles = 0
print(f'{player} rolled {x + y}')
player = next(turn)
print(f'It is {player}\'s turn\n')
if doubles == 3:
print(f'{player} rolled 3 Doubles! Go to jail.\n')
player = next(turn)
break
player1 rolled 3
It is player2's turn
player2 rolled 3
It is player1's turn
player1 folled 12, Doubles!
It is player1's turn
player1 folled 10, Doubles!
It is player1's turn
player1 folled 2, Doubles!
It is player1's turn
player1 rolled 3 Doubles! Go to jail.
所以我需要编写一个 Python 程序,我需要掷 2 个骰子并打印 2 个骰子的总和。到目前为止我得到了这个:
import random
def monopoly():
x = random.randrange(1,7)
y = random.randrange(1,7)
while True:
if x != y:
print(x, '+', y, '=', x+y)
break
现在,每次 2 个骰子的数字相同时(2 + 2 或 3 + 3 等),您可以再次掷出。如果连续 3 次骰子都相同,则需要进监狱。我以为我必须像这样使用 continue 来处理 while 循环:
else:
if x == y:
print(x + y)
continue
#continuation of the code above
现在,如果我确实得到了骰子相同的结果,它会一遍又一遍地打印出总和,直到我自己停止程序。但是不知道为什么。
我该如何解决这个问题?因为我不知道该怎么做。
您需要在 each 循环迭代中使用新的随机数:
while True:
x = random.randrange(1,7)
y = random.randrange(1,7)
if x != y:
print(x, '+', y, '=', x+y)
break
否则,x
和 y
永远不会改变,所以你的破坏条件永远不会成立。
程序继续循环的原因是因为它处于 while
循环中。
因为它总是True
,所以没有办法打破循环。
起初这可能很奇怪,但是当你看的时候,你会看到 x
和 y
是在循环外定义的,它们总是相同的。
因此,在它们相同的情况下,它总是相同的。
您必须在 else
部分或 while
语句的开头将 x
和 y
重新定义为不同的变量,以便生成新的这两个变量的值,否则每次都给出相同的值。
这是一个结构,您可以使用它来改变玩家的轮次,在两次掷骰之间,然后将玩家送进监狱以掷出 3 个双打。对于双打,我们可以使用 运行 计数,如果它命中 3 将 print('Go to jail')
。这是一个总体思路,供您使用
from random import choice
from itertools import cycle
die = [1, 2, 3, 4, 5, 6]
doubles = 0
players = cycle(['player1', 'player2'])
turn = iter(players)
player = next(turn)
while True:
x, y = choice(die), choice(die)
if x == y:
print(f'{player} folled {x + y}, Doubles!')
print(f'It is {player}\'s turn\n')
doubles += 1
else:
doubles = 0
print(f'{player} rolled {x + y}')
player = next(turn)
print(f'It is {player}\'s turn\n')
if doubles == 3:
print(f'{player} rolled 3 Doubles! Go to jail.\n')
player = next(turn)
break
player1 rolled 3 It is player2's turn player2 rolled 3 It is player1's turn player1 folled 12, Doubles! It is player1's turn player1 folled 10, Doubles! It is player1's turn player1 folled 2, Doubles! It is player1's turn player1 rolled 3 Doubles! Go to jail.