<p>这是操作顺序的问题:</p>
<ol>
<li>如果分数为负值,则将其重置为0</li>
<li>你掷骰子</li>
<li>应用+10或-5操作</li>
<li>打印结果,此时结果可能为负数</李>
</ol>
<p>所以,如果打印的分数有时是负片,这是正常的</p>
<p>以下是一个工作示例:</p>
<pre><code>import random
roundno = 0
p1score, p2score = 0, 0
p1dice1, p1dice2 = 0, 0
p2dice1, p2dice2 = 0, 0
while roundno < 6:
print("round number is round", roundno) #new round always begins on player 1 turn.
print("player 1")
p1dice1, p1dice2 = random.randint(1,6), random.randint(1,6) # roll dice using cpu.
# compare the score to see if odd or even.
if (p1dice1 + p1dice2) % 2 == 0:
p1score += 10
else:
p1score -= 5
if p1score < 0:
p1score = 0
print ("score is", p1score)
print("dice 1 is", p1dice1)
print("dice 2 is", p1dice2)
input("press enter to continue rolling dice.")
print ("player 2")
# roll dice using cpu.
p2dice1, p2dice2 = random.randint(1,6), random.randint(1,6)
# compare the score to see if odd or even.
if (p2dice1 + p2dice2) % 2 == 0:
p2score += 10
else:
p2score -= 5
if p2score < 0:
p2score = 0
print ("score is", p2score)
print("dice 1 is", p2dice1)
print("dice 2 is", p2dice2)
roundno = roundno+1
input("press enter to continue rolling dice.")
</code></pre>