繁体   English   中英

如何将我的骰子游戏中每一轮的分数相加?

[英]How to sum the scores from each round in my dice game?

我创建了这个骰子游戏。 用户说他们不想再次滚动后,如何计算每个回合的分数? 谢谢!!!

import colorama
colorama.init()

print_in_green = "\x1b[32m"
print_in_red = "\x1b[31m"
print_in_blue = "\x1b[36m"
print_in_pink = "\x1b[35m"
print_default = "\x1b[0m"

import random
min = 1
max = 6
game_response = input("Would you like to roll your dice (y/n)? ")

if game_response == "y":
    roll_again = "yes"

    while roll_again == "yes" or roll_again == "y":
        print("Rolling the dices...")
        print("The values are:")
        dice1 = random.randint(min, max)
        dice2 = random.randint(min, max)
        print(print_in_pink)
        print(int(dice1))
        print(int(dice2))
        print(print_default)
        score = (int(dice1) + int(dice2))
        roll_again = input("Your score for this round is " + str(score) + ". Roll the dices again (y/n)? ")
else:
    print("Ok!")

只需将总和存储在变量中 ,然后再打印,如下所示:

import colorama
colorama.init()

print_in_green = "\x1b[32m"
print_in_red = "\x1b[31m"
print_in_blue = "\x1b[36m"
print_in_pink = "\x1b[35m"
print_default = "\x1b[0m"

import random
min = 1
max = 6
game_response = input("Would you like to roll your dice (y/n)? ")

# Create variable to store the accumulated score
total_score = 0

if game_response == "y":
    roll_again = "yes"

    while roll_again == "yes" or roll_again == "y":
        print("Rolling the dices...")
        print("The values are:")
        dice1 = random.randint(min, max)
        dice2 = random.randint(min, max)
        print(print_in_pink)
        print(int(dice1))
        print(int(dice2))
        print(print_default)
        score = (int(dice1) + int(dice2))
        roll_again = input("Your score for this round is " + str(score) + ". Roll the dices again (y/n)? ")
        total_score = total_score + score
    print("Here is your score:",total_score)
else:
    print("Ok!")

只需将分数加起来即可。 在他们决定不再滚动后,打印总和。

 import colorama colorama.init() print_in_green = "\\x1b[32m" print_in_red = "\\x1b[31m" print_in_blue = "\\x1b[36m" print_in_pink = "\\x1b[35m" print_default = "\\x1b[0m" import random min = 1 max = 6 sum = 0 game_response = input("Would you like to roll your dice (y/n)? ") if game_response == "y": roll_again = "yes" while roll_again == "yes" or roll_again == "y": print("Rolling the dices...") print("The values are:") dice1 = random.randint(min, max) dice2 = random.randint(min, max) print(print_in_pink) print(int(dice1)) print(int(dice2)) print(print_default) score = (int(dice1) + int(dice2)) sum = sum + score roll_again = input("Your score for this round is " + str(score) + ". Roll the dices again (y/n)? ") else: print("Ok!") print(sum) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM