[Tutor] Silly Dice Game

John Connors oztriking at hotmail.com
Sat Apr 29 14:20:52 CEST 2006


G'day,

The wife and I play a silly dice game and I thought it would be a good 
challange for me to write it as a program. No specific question, just 
wondering if my technique is ok and is there anything that could be done 
better. The 2 hardest things I find as a python beginner who has no real 
need to learn a programming language other than curiosity and too much spare 
time, are thinking of something to program and then knowing if what I have 
written could be done more efficiently.



import random

def removeDice():
    for a in range(3):
        dieList.remove(checkThree)

while True:

    dieLeft = 6
    totalScore = 0

    while dieLeft > 0:

        dieList = []
        score = 0

        # roll the dice
        if dieLeft == 6:
            raw_input("\nPress enter to roll the dice ")
        print
        for dieRoll in range(dieLeft):
            dieList.append(random.randint(1,6))
            print "Die roll %d is %d" % (dieRoll +1, dieList[dieRoll])
        dieList.sort()

        if dieLeft == 6:

            #check if all dice are the same
            for fatChance in range(1,7):
                if dieList.count(fatChance) == 6:
                    score += 5000
                    dieLeft = 0
                    print "\nYou rolled all %d's, WoW!!!" % (fatChance)

            #check for a straight
            if dieList == [1,2,3,4,5,6]:
                score += 1500
                dieLeft = 0
                print "\nYou rolled a straight"

            #check for 3 pairs
            if dieLeft == 6:
                if dieList[0] == dieList[1] and dieList[2] == dieList[3] and 
dieList[4] == dieList[5]:
                    score += 1500
                    dieLeft = 0
                    print"\nYou rolled three pairs"

        #check for 3 of a kind
        if dieLeft > 2:

            for checkThree in range(1,7):

                if dieList.count(checkThree) >= 3:
                    print "\nYou rolled three %d's" % (checkThree)
                    yourChoice = raw_input("\nDo you wish to keep them? y/n 
")

                    if yourChoice == 'y':
                        dieLeft -= 3

                        if checkThree == 1:
                                score += 1000
                                removeDice()
                        else:
                            score = score + checkThree * 100
                            removeDice()

        #check for 1's and 5's
        if 1 in dieList or 5 in dieList:

            for testDice in range(dieLeft):

                if dieList[testDice] == 1:
                    score += 100
                    dieLeft -= 1

                if dieList[testDice] == 5:
                    score += 50
                    dieLeft -= 1

        totalScore = totalScore + score

        if score != 0:
            print "\nYour score is %d and you have %d dice left" % 
(totalScore, dieLeft)

        if score == 0:
            print "\nYou busted and score nothing"
            totalScore = 0
            break

        if dieLeft >= 1:
            rollAgain = raw_input ("\nRoll the remaining dice? y/n ")

            if rollAgain != "y":
                break

        if dieLeft == 0:
            dieLeft = 6
            print "\nYou used all the dice and must roll again"

    print "\n\nYour total score for that round was %d" % (totalScore)
    playAgain = raw_input("\nPlay again? (y/n) ")
    print

    if playAgain != "y":
        break



John

_________________________________________________________________
realestate.com.au: the biggest address in property   
http://ninemsn.realestate.com.au



More information about the Tutor mailing list