0

Here is my code from Python, I want occurence to be one big list. When I run it I just get back many one digit lists, I tried moving the print statement out of the loop, but I only get one answer. Any ideas?

import random

rolls = 0
occurrence = []

for i in range(0, 1000):
    dice1 = random.randrange(1, 7)
    dice2 = random.randrange(1, 7)
    rolls = rolls + 1
    occurrence = [dice1 + dice2]
    print(occurrence)    
1
  • O Okay, that works! Thanks everyone for the help :) Commented Feb 26, 2016 at 1:16

4 Answers 4

2

You can use append():

import random

rolls = 0
occurrence = []

for i in range(0, 1000):
    dice1 = random.randrange(1, 7)
    dice2 = random.randrange(1, 7)
    rolls = rolls + 1
    occurrence.append(dice1 + dice2)

print(occurrence)
Sign up to request clarification or add additional context in comments.

Comments

1

This will give you a list of length 2 tuples with random numbers between 1, and 7.

occurences = [(random.randrange(1, 7) , random.randrange(1, 7)) for i in range(1000)]

1 Comment

It is unclear whether OP wants tuples or sums of two dice.
0

A rather pythonic way would be to use a list comprehension:

occurence = [ (random.randrange(1,7)+random.randrange(1,7)) for _ in range(1000)]

Comments

0

From what I can tell from your question this should work:

import random

rolls = 0
occurence = []

for i in range(0,1000):
    dice1 = random.randrange(1,7)
    dice2 = random.randrange(1,7)
    rolls = rolls + 1
    occurence.append(dice1 + dice2)  #you need to add each value to the list with append
print (occurence)

When you use this:

occurence = [dice1 + dice2]

That creates a new list each time the for loop runs so the result at the end of the for loop is a list with a single number.

1 Comment

This will print the whole list in each loop iteration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.