簡體   English   中英

我如何將每一輪的總分相加

[英]how do i add up the total scores of each round

我創建了一個骰子游戲,它在每輪結束時顯示分數。 我想知道如何將兩個玩家的分數分別相加,以便顯示 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()

我如何將每一輪的分數相加? 謝謝

我建議您重新編寫代碼——例如,您可以使用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()

如果我的打印語句沒有意義, 可能很有用。

這是新打印語句的輸出。

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

編輯:在將分數添加到玩家dict之前添加if語句以檢查score是否為負。 如果為負 -> 改為添加0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM