Rolling 100-sided "dice" until they are the same number I've just begun my journey into python (1 week now) and I've written couple of lines incorporating everything I've learned so far. It would be greatly appreciated if anyone could guide me in writing a better formatted (appropriate style) version of this.

I've just written a couple of lines on finding two random rolls that stop when they roll the same number. When the same number is rolled, that number is recorded along with how many rolls it took to get there. I've also made the code to find those values and store them in a list, finding the average of roll number(same number) as well as roll count(How many rolls it took). Here is the code:

import random
totalrolls = 0
rollList = []
rollcountlist = []
while totalrolls &lt; 500:
    totalrolls = totalrolls + 1
    rollcount = 0
    roll = random.randint(1,100)
    rolltwo = random.randint(1,100)
    while roll != rolltwo:
        roll = random.randint(1,100)
        rolltwo = random.randint(1,100)
        #print roll
        #print rolltwo
        rollcount = rollcount + 1
    if roll == rolltwo:
        rollList.append(roll)
        rollcountlist.append(rollcount)
        print "Random number that matched was: %s, after %s rolls. " % (roll, rollcount)
if totalrolls == 500:
    print "Finished rolling %s times." % totalrolls

averageroll = sum(rollList) / len(rollList)
averagerollcount = sum(rollcountlist) / len(rollcountlist)

print rollList
print rollcountlist
print averageroll
print averagerollcount
You have an off-by-one error.  If you get lucky and get a matching pair immediately, then you would report "Random number that matched was: …, after 0 rolls."  I would say that "after 1 roll(s)" would be the right way to report that.  (Alternatively, you could phrase it as "after 0 mismatched rolls".)

Naming could be improved slightly.  I suggest:


rollcountlist → roll_counts, because it's shorter and more readable.  Since englishisnotgerman, you should use an underscore, as recommended by PEP 8.
rollList → roll_values, because "values" makes it clearer what is being stored.  Watch your capitalization consistency.
averageroll → average_value and averagerollcount → average_count, for similar reasons.
totalrolls → experiment_count, because totalrolls sounds like it represents a cumulative sum of rollcount, which it isn't.  Introducing the term "experiment" makes it clear that you are counting something else.
roll → roll1 and rolltwo → roll2, for consistency.  Or, as I've done below, make one two-element tuple called rolls.


The if conditions after the while loops are superfluous.  The inner loop can only be exited when the roll values match.  The outer loop can only be exited after 500 experiments.

Counting loops can be more elegantly expressed.  For a fixed number of iterations, such as for the outer loop, use the idiom for i in xrange(n): ….  For an unbounded count, such as for the inner loop, use for i in itertools.count().

In Python 2, the / operator performs integer division (rounding down) when dividing an integer by an integer.  For a more accurate mean, convert at least one of the operands to a float first.

When interpolating an integer into a format string, use the %d placeholder instead of %s for clarity.

from itertools import count
import random

roll_values = []
roll_counts = []
for experiment_count in xrange(1, 501):
    for roll_count in count(1):
        rolls = (random.randint(1,100), random.randint(1, 100))
        if rolls[0] == rolls[1]:
            break
    roll_values.append(rolls[0])
    roll_counts.append(roll_count)
    print "Random number that matched was: %d, after %d rolls." % (rolls[0], roll_count)

average_value = float(sum(roll_values)) / len(roll_values)
average_count = float(sum(roll_counts)) / len(roll_counts)

print "Finished rolling %d times." % experiment_count    
print roll_values
print roll_counts
print average_value
print average_count