2 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
你可以簡單地這樣寫:
if pl1_vote == 2 and pl2_vote == 2:
if random.random() < 0.5:
pl1_vote += 1
else:
pl2_vote += 1

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
您走在正確的軌道上——我可以看到您正在嘗試采用數(shù)據(jù)驅(qū)動(dòng)的方法。但是,由于您的投票作為兩個(gè)單獨(dú)的變量存在,您有點(diǎn)聽天由命地做這樣的事情:
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
數(shù)據(jù)驅(qū)動(dòng)的方法會(huì)將選票合并到某種集合中。這是一種方法:
from random import randint
player_votes = [2, 2]
if player_votes[0] == player_votes[1]:
player_votes[randint(0, len(player_votes)-1)] += 1
添加回答
舉報(bào)