Python Forum

Full Version: Sum of numbers from for loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,

I am trying to create a dice roll game where 2 players can roll 2 dice each for 5 rounds and then their points are added up. I am trying to use a for loop, but can't seem to find a way to add up the numbers from those 5 rounds. This is what I have so far:

for i in range(5):
  die1 = random.randint(1,6)
  die2 = random.randint(1,6)
  diesum = die1 + die2
  print("Your score for this round is",diesum)
How can I calculate the sum of all five 'diesum's ?
something like
import random
diesum = 0
for i in range(5):
    die1 = random.randint(1,6)
    die2 = random.randint(1,6)
    diesum += die1 + die2
print("Your score for this round is {}".format(diesum))
if you don't need the numbers from individual dies
import random
diesum = sum([random.randint(1,6) for _ in range(10)])
print("Your score for this round is {}".format(diesum))
Hello buran, I already know how to add up the scores in one round, I'm trying to add up the scores of all 5 rounds...
that's exactly what my code snippets do.
Here is code snippet with extra print at each iteration of the loop
import random
diesum = 0
for i in range(5):
    die1 = random.randint(1,6)
    die2 = random.randint(1,6)
    diesum += die1 + die2
    print('die1: {}, die2: {}, current diesum: {}'.format(die1, die2, diesum))
print("Your score for this round is {}".format(diesum))
I'm sorry if I didn't explain my issue well enough, but this is not quite what I need. I want the user to see the score for each individual round AND the total score for all 5 rounds. The two solutions that you have given me do only one of those things, but I need them to do both.
I have added a code snippet to my previous post