我怎样把每轮的总分加起来

2024-10-01 17:22:45 发布

您现在位置: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()

我怎样才能把每一轮的分数相加呢? 谢谢


Tags: youtimecountsleeprandomdicescoreplayer
1条回答
网友
1楼 · 发布于 2024-10-01 17:22:45

我建议您重新编写代码,例如,您可以使用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如果我的print语句没有意义,可能会有用。你知道吗

这是新print语句的输出。你知道吗

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语句,以检查score是否为负。如果为负->;则添加0。你知道吗

相关问题 更多 >

    热门问题