Anna Hamelin
Anna Hamelin

Reputation: 39

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

I created this dice game. How can I sum the score from each round after the user says they do not want to roll again? Thank you!!!

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!")

Upvotes: 0

Views: 1200

Answers (2)

Manrique
Manrique

Reputation: 2231

Just store the sum in a variable, and print it later, like this:

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!")

Upvotes: 2

MIKEC
MIKEC

Reputation: 146

Just add the score up to sum. And print the sum after they decide not to roll any more.

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)

Upvotes: 0

Related Questions