我创建了一个骰子游戏,该骰子在每个回合结束时显示得分。我想知道如何分别将两个球员的得分相加,以便显示5个回合的总得分。
这是下面的代码:
import random
import time
def bothDice():
count=0
while count<5:
count=count+1
score=0
print("Round",count)
print("Player One rolls first dice")
time.sleep(1)
dice1=(random.randint(1,6))
print("you rolled a ",dice1)
print("Player One rolls second dice")
time.sleep(1)
dice2=(random.randint(1,6))
print("you rolled a ",dice2)
score=dice1+dice2
if score%2==0:
score=score+10
if dice1==dice2:
print("You rolled a double- You get an extra roll")
for x in range (1):
print("You rolled a:")
extraDice=(random.randint(1,6))
print(extraDice)
extraScore = score+extraDice
score = extraScore
else:
score=score-5
print("======","Your combined score is ", score,"======")
score=0
print("Player Two rolls first dice")
time.sleep(1)
dice1=(random.randint(1,6))
print("you rolled a ",dice1)
print("Player Two rolls second dice")
time.sleep(1)
dice2=(random.randint(1,6))
print("you rolled a ",dice2)
score=dice1+dice2
if score%2==0:
score=score+10
if dice1==dice2:
print("You rolled a double- You get an extra roll")
for x in range (1):
print("You rolled a:")
extraDice=(random.randint(1,6))
print(extraDice)
extraScore = score+extraDice
score = extraScore
else:
score=score-5
print("======","Your combined score is ", score,"======")
def main():
bothDice()
main()
我如何使每回合的分数相加? 谢谢
答案 0 :(得分:0)
我建议您重新编写代码-例如,您可以使用for
循环而不是while
循环来跟踪回合。我认为使用dict
或list
是有用的。我为两个球员result_p1
和result_p2
添加了两个字典,每局存储分数。分数可以为负(不确定是否要这样做)。
这是下面的代码:
import random
import time
def bothDice():
count=0
# New dicts that store counts
result_p1 = {}
result_p2 = {}
while count<5:
count=count+1
score=0
print("Round",count)
print("Player One rolls first dice")
time.sleep(1)
dice1=(random.randint(1,6))
print("you rolled a ",dice1)
print("Player One rolls second dice")
time.sleep(1)
dice2=(random.randint(1,6))
print("you rolled a ",dice2)
score=dice1+dice2
if score%2==0:
score=score+10
else:
score=score-5
if dice1==dice2:
print("You rolled a double- You get an extra roll")
for x in range (1):
print("You rolled a:")
extraDice=(random.randint(1,6))
print(extraDice)
extraScore = score+extraDice
score = extraScore
print("======","Your combined score is ", score,"======")
# Store result of this round for player 1
if score != 0:
result_p1[count] = score
else:
result_p1[count] = 0
score=0
print("Player Two rolls first dice")
time.sleep(1)
dice1=(random.randint(1,6))
print("you rolled a ",dice1)
print("Player Two rolls second dice")
time.sleep(1)
dice2=(random.randint(1,6))
print("you rolled a ",dice2)
score=dice1+dice2
if score%2==0:
score=score+10
else:
score=score-5
if dice1==dice2:
print("You rolled a double- You get an extra roll")
for x in range (1):
print("You rolled a:")
extraDice=(random.randint(1,6))
print(extraDice)
extraScore = score+extraDice
score = extraScore
print("======","Your combined score is ", score,"======")
# Store result of this round for player 2
if score != 0:
result_p2[count] = score
else:
result_p2[count] = 0
# Print sum of results using f-string in python 3.
print(f"Player 1 scores: {result_p1}")
print(f"Player 2 scores: {result_p2}")
print(f"Sum of player 1 score: {sum(result_p1.values())}")
print(f"Sum of player 2 score: {sum(result_p2.values())}")
def main():
bothDice()
main()
如果我的打印声明没有意义,This可能会有用。
这是新打印语句的输出。
Player 1 scores: {1: 9, 2: 13, 3: 17, 4: 0, 5: 19}
Player 2 scores: {1: 17, 2: 13, 3: 13, 4: -2, 5: -2}
Sum of player 1 score: 58
Sum of player 2 score: 39
编辑:添加if
条语句,然后向玩家dict
添加得分,以检查score
是否为负数。如果为负,请添加0
。