Backjack program

This is the place for queries that don't fit in any of the other categories.

Backjack program

Postby lundyman » Thu Dec 05, 2013 9:42 pm

Can someone please fix this code and tell me what it does? I will be eternally grateful...

Code: Select all
from random import randrange

def main():
    print "Simulation of a Blackjack dealer.\n"
    n = input("How many trials? ")
    busts = 0
    for i in range(n):
        points = dealHand()
        if points > 21:
            busts = busts + 1
    print "In %d hands dealer busted %d times." % (n, busts)
    print "Estimated prob =", float(busts)/n

def dealHand():
    total = 0
    haveAce = False
    while total < 17:
        card = randrange(1,14)
        if card == 1:
           haveAce = True
        total = total + BJValue(card)
        if haveAce:
            total = adjustForAce(total)
    return total
def BJValue(card):
    if card > 10:
        return 10
        return card

def adjustForAce(total):
    if 16 < total + 10 < 22:
        return total + 10
        return total

if __name__ == '__main__':
Last edited by micseydel on Thu Dec 05, 2013 9:52 pm, edited 2 times in total.
Reason: Code tags, locked OP, better title.
Posts: 1
Joined: Thu Dec 05, 2013 9:38 pm

Re: Backjack program

Postby micseydel » Thu Dec 05, 2013 9:50 pm

Hello, and welcome to the forum. Make sure to read this before making your next post.

Added in edit: make sure to make an effort toward academic honesty here. We don't want to ruin your Ultimate career but clear "do it for me" posts will have their schools contacted.
Join the #python-forum IRC channel on for off-topic chat!

Please prefer not to PM members. The point of the forum is so that anyone can benefit. We don't want to help you over PMs/emails/Skype chats that others can't benefit from :)
User avatar
Posts: 2916
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 5 guests