So I was going through the Projects for the Beginner thread. I settled on this:

"For $2 you get to throw four dice. If the sum of the faces is less than 9, you win $12, otherwise you lose your investment. Should you play this game?"

Simple enough:

def sum(list):
	sum = 0
	for  elem in list:
		sum += elem
	return sum
def findOdds():
	possibles = [ [1,1,1,1], [1,1,1,2], [1,1,1,3], [1,1,1,4], [1,1,1,5], [1,1,1,6], \
	[1,1,2,6], [1,1,3,6], [1,1,4,6], [1,1,5,6], [1,1,6,6], \
	[1,2,6,6], [1,3,6,6], [1,4,6,6], [1,5,6,6], [1,6,6,6], \
	[2,6,6,6], [3,6,6,6], [4,6,6,6], [5,6,6,6], [6,6,6,6] ]
	winners = [roll for roll in possibles if sum(roll) < 9]
	return len(possibles) / len(winners) * 100.0
if __name__ == "__main__":
	print "Only %f %% chance at winning\n" % findOdds()
File "dicegame.py", line 1
        possibles = [ [1,1,1,1], [1,1,1,2], [1,1,1,3], [1,1,1,4], [1,1,1,5], [1,
        print "Only %f %% chance at winning\n" % findOdds()< 9]\
SyntaxError: invalid syntax

That's greek to me. The backslash... thing... bit me before, so I checked that--thinking maybe I didn't hit return immediately after. That's not it, and my list looks fine. Hopefully you people will see something I don't.

I'm using python 2.2.1

9 Years
Discussion Span
Last Post by Murtan

That code doesn't give a syntax error with python 2.5.4 or 2.6.1.

And you should really consider upgrading. That version is over six years old!


Upgrading seems like it would be very helpful. For one thing, I dont have to write sum myself in later versions. I'll do that then.

Thanks everyone.


I was interested in the program though and i made my own that tests a more real life scenario as in that it uses random numbers

import random
def getRoll():
    return [random.randrange(1,7) for f in range(4)]

def win(l):
    s = sum(l)
    if s < 12:
        return True
        return False
def main(times):
    money = 1000
    for f in range(times):
        money -=2
        if win(getRoll()):
            money += 12
    print "You ended up with %s dollars after %s turns" %(money, times)
if __name__ == "__main__":
    times = int(raw_input("How many times would you like the test to be run?"))

This would output something like this

How many times would you like the test to be run?100000
You ended up with 89072 dollars after 100000 turns

After running it through a number of times i have found that i always seem to win, so if this game was real i think i would play!


Also don't go straight for python 3 because it's the latest version. Look through the docs and see what has changed before you make the leap.


Just a comment:

return len(possibles) / len(winners) * 100.0

Calculated the wrong percentage

I changed it to:

return 100.0 * len(winners) / len(possibles)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.