I am writing a dice rolling simulation and i need help redefining a variable inside a loop.

Recommended Answers

All 4 Replies

my code so far is:

import random
import time

print "Dice Roll Program"
print "\nProgrammed by Devon McAvoy"

r = int(raw_input("\nNumber of dice to roll: "))
n = int(raw_input("\nNumber of times to roll the dice (die): "))

print "\n"

f = open('/home/kerry/dice roll/dice roll.txt', 'w')

f.write('Dice Roll Program\n')
f.write('Dice Roll Programmed by Devon McAvoy\n')

rstr = repr(r) + 'dice (die) were rolled.'
nstr = 'The dice (die) was rolled ' + repr(n) + ' times.'

f.write(rstr)
f.write('\n')
f.write(nstr)
f.write('\n')

one = 0
two = 0
three  = 0
four = 0
five = 0
six = 0
counter = 0
dice = 0
roll = random.randint(1,6)

while (dice < r):
    while (counter < n):
        if roll == 1:
            one += 1
            if one > 0:
                print "1"
                f.write('1,')
                time.sleep(.2)
            counter += 1
        elif roll == 2:
            two += 1
            if two > 0:
                print "2"
                f.write('2,')
                time.sleep(.2)
            counter += 1
        elif roll == 3:
            three += 1
            if three > 0:
                print "3"
                f.write('3,')
                time.sleep(.2)
            counter += 1
        elif roll == 4:
            four += 1
            if four > 0:
                print "4"
                f.write('4,')
                time.sleep(.2)
            counter += 1
        elif roll == 5:
            five += 1
            if five > 0:
                print "5"
                f.write('5,')
                time.sleep(.2)
            counter += 1
        else:
            six += 1
            if six > 0:
                print "6"
                f.write('6,')
                time.sleep(.2)
            counter += 1
    if counter == n:
        dice += 1
        counter = 0
        print "\n"
        f.write('\n')
        

print "\nThe dice (die) landed on one", one, "times."
print "\nThe dice (die) landed on two", two, "times."
print "\nThe dice (die) landed on three", three, "times."
print "\nThe dice (die) landed on four", four, "times."
print "\nThe dice (die) landed on five", five, "times."
print "\nThe dice (die) landed on six", six, "times."

the problem is i don't know how to redefine roll so it picks a different # every time it loops. everything i have tried inserting in the loop:

roll = random.randint(1,6)

def roll:
    random.randint(1,6)

pulls a syntax error on me.

I am alternating two machines. one running ubuntu 8 the other XP. Ubuntu has python 2.5 and the XP has 2.6.

Put the roll statement under one of the while loops
roll = random.randint(1,6)
Now, it is only executed once at the beginning of the program. Also, instead of all of the if/elif you can use a list or dictionary and increment, for example
counter_list[roll] += 1
You will have to initialize it to zero first of course, where you are now initializing the 6 counters. Finally, the "if one > 0" statement is not necessary. The counter will always be greater than zero since you add one on the previous line

while (dice < r):
    while (counter < n):
        if roll == 1:
            one += 1
            if one > 0:
                print "1"
                f.write('1,')
                time.sleep(.2)
            counter += 1
#
#   so you can just do something like
while (dice < r):
    while (counter < n):
        ##   also I would __strongly__ suggest adding this print statement
        print "inner while loop = %d of %d" % (counter, n)
        roll = random.randint(0,7)
        counter_list[roll] += 1
        print roll
        f.write("%d\n" % (roll))
        time.sleep(0.2)
        counter += 1

Thanks,

I didn't use your exact suggestion but line 15 of you suggested code gave me an idea that was far easier than rewriting my entire program.

while (dice < r):
    while (counter < n):
        roll = random.randint(1,6)
        if roll == 1:
            one += 1
            if one > 0:
                print "1"
                f.write('1,')
                time.sleep(.2)
            counter += 1
            roll = random.randint(0,7)

as you can see i moved my line 33 into the 2nd loop and then add your line 15 to the end of my 3rd loop to so it redefined roll when ever it chose a number and then when it looped redefined it again back to the original definition...

go confusing loops...........

I hope you also took the suggestion to add the print statement for testing. And random.randint should use 1 & 6 (sorry, my mistake). But in the following code you have that statement twice, with different values for each. The second time will never be used. At the start of the next loop, the statement directly after the while() statement will overlay it with a new value for roll. The program is a little too verbose but does the job and that's what counts.

while (dice < r):
    while (counter < n):
        roll = random.randint(1,6)
        if roll == 1:
            one += 1
            if one > 0:
                print "1"
                f.write('1,')
##   these 3 statements are the same for every if/else and
##   so should be deleted from here and all of the other
##   elif.  You can use one set of  these statements after 
##   all of the if/elif since they are to be executed no
##   matter what the value of roll is.
                time.sleep(.2)
            counter += 1
            roll = random.randint(0,7)   ##  redundant 2nd statement
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.