我如何将每一轮的总成绩相加

时间:2019-01-16 21:29:17

标签: python

我创建了一个骰子游戏,该骰子在每个回合结束时显示得分。我想知道如何分别将两个球员的得分相加,以便显示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()

我如何使每回合的分数相加? 谢谢

1 个答案:

答案 0 :(得分:0)

我建议您重新编写代码-例如,您可以使用for循环而不是while循环来跟踪回合。我认为使用dictlist是有用的。我为两个球员result_p1result_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