# dice game, removing dice each loop and looping.

Page 1 of 1

## 2 Replies - 2981 Views - Last Post: 24 October 2011 - 01:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=252577&amp;s=787aa0e1e49cba28989b4d93b758b073&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 emery

Reputation: 0
• Posts: 4
• Joined: 22-October 11

# dice game, removing dice each loop and looping.

Posted 23 October 2011 - 09:21 AM

in this program, i have a single trial set up to roll 6 dice, add the scores except for the 2's and 5's and then remove the dice than landed on 2's and 5's and repeat until there are no dice left, and the final score has been tabulated. I have 2 'main' variables score and remove. score adds the score from the roll as an int, and holds that value, do i need to set these as global variables and then call for the global versions in each def function? as well for die1-die5? i tired that and didn't seem to get anywhere.

```
import random

def rolldice():

die1 = random.randint(1,6)
die2 = random.randint(1,6)
die3 = random.randint(1,6)
die4 = random.randint(1,6)
die5 = random.randint(1,6)

def singletrial():

rolldice()
score = score + int(die1 + die2 + die3 + die4 + die5)

if die1 == 2:
score = score-2
remove = remove+1
elif die2 == 2:
score = score-2
remove = remove+1
elif die3 == 2:
score = score-2
remove = remove+1
elif die4 == 2:
score = score-2
remove = remove+1
elif die5 == 2:
score = score-2
remove = remove+1
elif die1 == 5:
score = score-5
remove = remove+1
elif die2 == 5:
score = score-5
remove = remove+1
elif die3 == 5:
score = score-5
remove = remove+1
elif die4 == 5:
score = score-5
remove = remove+1
elif die5 == 5:
score = score-5
remove = remove+1
else:
pass

if remove == 0:
singletrial()
if remove == 1:
remove1()
if remove == 2:
remove2()
if remove == 3:
remove3()
if remove == 4:
remove4()
if remove == 5:
remove5()

def remove1():

rolldice()

score = score + int(die1 + die2 + die3 + die4)

if die1 == 2:
score = score-2
remove = remove+1
elif die2 == 2:
score = score-2
remove = remove+1
elif die3 == 2:
score = score-2
remove = remove+1
elif die4 == 2:
score = score-2
remove = remove+1
elif die1 == 5:
score = score-5
remove = remove+1
elif die2 == 5:
score = score-5
remove = remove+1
elif die3 == 5:
score = score-5
remove = remove+1
elif die4 == 5:
score = score-5
remove = remove+1
else:
pass

if remove == 0:
remove1()
if remove == 1:
remove2()
if remove == 2:
remove3()
if remove == 3:
remove4()
if remove == 4:
remove5()

return score, remove

def remove2():

rolldice()

score = score + int(die1 + die2 + die3)

if die1 == 2:
score = score-2
remove = remove+1
elif die2 == 2:
score = score-2
remove = remove+1
elif die3 == 2:
score = score-2
remove = remove+1
elif die1 == 5:
score = score-5
remove = remove+1
elif die2 == 5:
score = score-5
remove = remove+1
elif die3 == 5:
score = score-5
remove = remove+1
else:
pass

if remove == 0:
remove2()
if remove == 1:
remove3()
if remove == 2:
remove4()
if remove == 3:
remove5()

return score, remove

def remove3():

rolldice()

score = score + int(die1 + die2)

if die1 == 2:
score = score-2
remove = remove+1
elif die2 == 2:
score = score-2
remove = remove+1
elif die1 == 5:
score = score-5
remove = remove+1
elif die2 == 5:
score = score-5
remove = remove+1
else:
pass

if remove == 0:
remove3()
if remove == 1:
remove4()
if remove == 2:
remove5()

return score, remove

def remove4():

rolldice()

score = score + int(die1)

if die1 == 2:
score = score-2
remove = remove+1
elif die1 == 5:
score = score-5
remove = remove+1
else:
pass

if remove == 0:
remove4()
if remove == 1:
remove5()

return score, remove

def remove5():

print score

def main():

numtrials = int(raw_input("Number of trials: "))

singletrial()

main()

```

Is This A Good Question/Topic? 0

## Replies To: dice game, removing dice each loop and looping.

### #2 Martyr2

• Programming Theoretician

Reputation: 5179
• Posts: 13,901
• Joined: 18-April 07

## Re: dice game, removing dice each loop and looping.

Posted 23 October 2011 - 09:59 AM

Ok between this and your other program I see you are having trouble understanding the concept of scope. So let me explain this as simple as I can...

You see where you define die1, die2... die5 in rolldice()? These are local variables. Meaning they only exist inside that function. When the function ends, those variables go what we call "out of scope". Meaning they can not be accessed by another function (like singletrial()).

There are three main ways to get the values out of a function so they can be used by others.

1) You define them up top as global. When you do this, if you wish to modify the values from within a function (like rolldice()) you must precede the variable with the keyword "global". This says "I want to use the global version previously defined, don't create a local version for me."

2) You return the value from the function using the return keyword. If you want to return a single value (or a single structure/class that contains multiple values like returning a list) you can return it using the return keyword and then where you call rolldice() (in singletrial) you would store the result... returnedValue = rolldice().

3) You can call another method, pass it a mutable object (an object that can be modified like a list, but not like a tuple or string) and the function can alter it. When the function ends, the list is then modified.

So now you have to decide which method works best for your design. Because obviously die1 and the others aren't going to be seen by the other functions. The easiest way (but often considered the most dangerous way) is by making die1 through die5 global and then in each function that wants to alter these variables must define them using the "global" keyword.

If you are not sure what global does, you can do a search on the net for "Python global keyword". One site that simply explains it is http://zetcode.com/t...rial/functions/
just scroll down to the part that is titled "global and local variables".

Hope this clears things up a bit.

This post has been edited by Martyr2: 23 October 2011 - 10:02 AM

### #3 McSick

Reputation: 33
• Posts: 179
• Joined: 02-September 10

## Re: dice game, removing dice each loop and looping.

Posted 24 October 2011 - 01:36 PM

You may also want to consider just hold the number of dice to a value rather than 6 dice. Here comes some psuedo code ^.^ Please note this probably wont run in python as my syntax is a little rusty and hell this algorithm may not work. I am just giving my insight.

```D=6
score=0
while(D>0):
roll = random.randint(1,6)
if(roll == 2 or roll == 5):
D -= 1
else:
score+=roll
print "OMG YOU JUST GAINED ", roll," points making your total ", score
pause(1000)

```

Then you can go beyond and make it so that the amount of dice starting from user input. Or better yet change it up to choose what values cause removal, how many sided dice you want, make specials, cause factors like wind speed that will favor number over others.....AI, ECT!!! PROGRAMMING IS INFINITE MWAHAHAHA *Cough* *Cough* but please be sure to understand the basics. Algorithm design is key. While it may work one way, you may save yourself hundreds of lines of code a different way. ^.^

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }