python 中的随机增量
Random increment in python
这里是初学者,我如何在 python、"in case of a tie,randomly increment one of the two elements?" 中表达它?
从 python 我得到 SyntaxError: can't assign to function call。如果您能提供任何帮助,我们将不胜感激。
if pl1_vote == 2 and pl2_vote == 2:
random.sample(pl1_vote, pl2_vote) += 1
您走在正确的轨道上 - 我可以看出您正在尝试采用数据驱动的方法。但是,由于您的投票作为两个单独的变量存在,您有点听天由命地做这样的事情:
from random import choice
pl1_vote = 2
pl2_vote = 2
if pl1_vote == pl2_vote:
if choice([True, False]):
pl1_vote += 1
else:
pl2_vote += 1
数据驱动的方法会将选票合并到某种集合中。这是一种方法:
from random import randint
player_votes = [2, 2]
if player_votes[0] == player_votes[1]:
player_votes[randint(0, len(player_votes)-1)] += 1
你可以简单明确地写成这样:
if pl1_vote == 2 and pl2_vote == 2:
if random.random() < 0.5:
pl1_vote += 1
else:
pl2_vote += 1
这里是初学者,我如何在 python、"in case of a tie,randomly increment one of the two elements?" 中表达它? 从 python 我得到 SyntaxError: can't assign to function call。如果您能提供任何帮助,我们将不胜感激。
if pl1_vote == 2 and pl2_vote == 2:
random.sample(pl1_vote, pl2_vote) += 1
您走在正确的轨道上 - 我可以看出您正在尝试采用数据驱动的方法。但是,由于您的投票作为两个单独的变量存在,您有点听天由命地做这样的事情:
from random import choice
pl1_vote = 2
pl2_vote = 2
if pl1_vote == pl2_vote:
if choice([True, False]):
pl1_vote += 1
else:
pl2_vote += 1
数据驱动的方法会将选票合并到某种集合中。这是一种方法:
from random import randint
player_votes = [2, 2]
if player_votes[0] == player_votes[1]:
player_votes[randint(0, len(player_votes)-1)] += 1
你可以简单明确地写成这样:
if pl1_vote == 2 and pl2_vote == 2:
if random.random() < 0.5:
pl1_vote += 1
else:
pl2_vote += 1