Learn Python – Code Example – BlackJack

24
1



Learn Python – BlackJack

In this video:
– Functions
– Nested for loops
– While loops
– If statements
– Comments

— Welcome to Coding4Chicks —

I am a programming chick with 10 years of experience in programming and the administration of computer systems. I have a masters in Software Engineering, a bachelors in Computer Science and have been teaching at University.

I dedicate this channel to my Chicas around the world that would also like to learn programming but don’t know where to start. This channel is in no way an indication that you will need special help in learning anything, sometimes we just feel like listening to a girl.

P.S. Boys, feel free to enjoy the site too! 😉

Remember: The sky is the limit. You can do anything you want!

Nguồn: https://khudothidongvan.com/

Xem thêm bài viết khác: https://khudothidongvan.com/game/

24 COMMENTS

  1. Hahaha dark, handsome, intelligent so all women like bbc's well if so i'm fucked #asiangang #tinydickgang
    But gotta say cool video

  2. Wait I have a questions if for your assignment your not allowed to use .pop what else can you use for that?

  3. Can someone please fix this im using pycharm btw

    from random import shuffle

    def deck():
    deck = []
    for suit in ['H️', '️S', 'D', 'C️']:
    for rank in ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']:
    deck.append(suit + rank)

    shuffle(deck)

    return deck

    def pointCount(myCards):
    myCount = 0
    aceCount = 0

    for i in myCards:
    if (i[1] == 'J' or i[1] == 'Q' or i[1] == 'K' or i[1] == 'T'):
    myCount += 10
    elif (i[1] != 'A'):
    myCount += int(i[1])
    else:
    aceCount += 1

    if (aceCount == 1 and myCount >= 10):
    myCount += 11
    elif(aceCount != 0):
    myCount += 1

    return myCount

    def createPlayingHands(myDeck):
    dealerHand = []
    playerHand = []
    dealerHand.append(myDeck.pop())
    dealerHand.append(myDeck.pop())
    playerHand.append(myDeck.pop())
    playerHand.append(myDeck.pop())

    while (pointCount(dealerHand) <= 15):
    dealerHand.append(myDeck.pop())

    return [dealerHand, playerHand]

    game = ""
    myDeck = deck()
    hands = createPlayingHands(myDeck)
    dealer = hands[0]
    player = hands[1]

    while (game != "exit"):
    dealerCount = pointCount(dealer)
    playerCount = pointCount(player)

    print "Dealer has:"
    print dealer[0]

    print "Player1, you have:"
    print player

    if (playerCount == 21):
    print "You Won!"
    break
    elif (playerCount > 21):
    print "Player BUSTS with " + str(playerCount) + " points. Dealer wins."
    break
    elif (dealerCount > 21):
    print "Dealer BUSTS with " + str(playerCount) + " points. Player wins!"
    break

    game = raw_input("Stand (S) or Hit (H)? ")

    if (game == 'H'):
    player.append(myDeck.pop())
    elif (playerCount > dealerCount):
    print "Player wins with " + str(playerCount) + " points"
    print "Dealer has: " + str(dealer) + " or " + str(dealerCount) + "points"
    break
    else:
    print "Dealer wins."
    print "Dealer has: " + str(dealer) + " or " + str(dealerCount) + "points"
    break

  4. https://github.com/osk4r99/School-projekt/blob/master/blackjack/blackjack(pyqt).py
    Here is great graphical blackjack game if anyone interested

  5. I have a problem when I "run the module" and I have this error: myCount += int(i[1])
    ValueError: invalid literal for int() with base 10: 'T'

  6. dealerHand.append(myDeck.pop()) [LINE 41]
    #i dont get it this part. im having an error. "IndexError: pop from empty list"
    please help me

  7. #This is what I did

    import tkinter
    import random

    def load_images(card_images):
    suits = ['heart', 'club', 'diamond', 'spade']
    face_card = ['jack', 'queen', 'king']

    if tkinter.TkVersion >= 8.6:
    extension = 'png'
    else:
    extension = 'ppm'

    # For each suit retrieve the image of the card
    for suit in suits:
    # First the number cards 1 to 10
    for card in range(1, 11):
    name = 'cards/{}_{}.{}'.format(str(card), suit, extension)
    image = tkinter.PhotoImage(file=name)
    card_images.append((card, image))
    # for the face cards
    for card in face_card:
    name = 'cards/{}_{}.{}'.format(str(card), suit, extension)
    image = tkinter.PhotoImage(file=name)
    card_images.append((10, image))

    def deal_card(frame):
    # Pop the next card off the top of the deck
    next_card = deck.pop(0)
    # add the image to a label and display the label
    tkinter.Label(frame, image=next_card[1], relief='raised').pack(side='left')
    # now return the card's face value
    return next_card

    def score_hand(hand):
    # Calculate the total score of all cards in the list
    # Only one ace can have the value of 11, and this will be reduced to 1 if the hand would bust.
    score = 0
    ace = False
    for next_card in hand:
    card_value = next_card[0]
    if card_value == 1 and not ace:
    ace = True
    card_value = 11
    score += card_value
    # if there is a bust, check if there is an ace and subtract 10
    if score > 21 and ace:
    score -= 10
    ace = False
    return score

    def deal_dealer():
    dealer_score = score_hand(dealer_hand)
    while 0 < dealer_score < 17:
    dealer_hand.append(deal_card(dealer_card_frame))
    dealer_score = score_hand(dealer_hand)
    dealer_score_label.set(dealer_score)

    player_score = score_hand(player_hand)
    if player_score > 21:
    result_text.set("Beta jaake ludo khel!")
    elif dealer_score > 21 or dealer_score < player_score:
    result_text.set("Jeetne diya tujhe")
    elif dealer_score > player_score:
    result_text.set("Chotu, baccho ki cheez nahi hain ye!")
    else:
    result_text.set("Bach gaya bete!")

    def deal_player():
    player_hand.append(deal_card(player_card_frame))
    player_score = score_hand(player_hand)

    # global player_score
    # global player_ace
    # card_value = deal_card(player_card_frame)[0]
    # if card_value == 1 and not player_ace:
    # player_ace = True
    # card_value = 11
    # player_score += card_value
    # # If we would bust check if there is an ace and subtract
    # if player_score > 21 and player_ace:
    # player_score -= 10
    # player_ace = False
    player_score_label.set(player_score)
    if player_score > 21:
    result_text.set("Chal nikal yaha se!")

    # Set up the screen and frames for the dealer and player

    mainWindow = tkinter.Tk()

    mainWindow.title("Laathi Khel")
    mainWindow.geometry("640×480")
    mainWindow.configure(background='green')

    result_text = tkinter.StringVar()
    result = tkinter.Label(mainWindow, textvariable=result_text, borderwidth=5)
    result.grid(row=0, column=0, columnspan=3)

    card_frame = tkinter.Frame(mainWindow, relief='sunken', borderwidth=1, background='green')
    card_frame.grid(row=1, column=0, sticky='ew', columnspan=3, rowspan=2)

    dealer_score_label = tkinter.IntVar()
    tkinter.Label(card_frame, text='Dealer', background='green', fg='white').grid(row=0, column=0)
    tkinter.Label(card_frame, textvariable=dealer_score_label, background='green', fg='white').grid(row=1, column=0)

    # embedded frame to hold the card images
    dealer_card_frame = tkinter.Frame(card_frame, background='green')
    dealer_card_frame.grid(row=0, column=1, sticky='ew', rowspan=2)

    player_score_label = tkinter.IntVar()
    tkinter.Label(card_frame, text='Player', background='green', fg='white').grid(row=2, column=0)
    tkinter.Label(card_frame, textvariable=player_score_label, background='green', fg='white').grid(row=3, column=0)

    # embedded frame to hold the card images
    player_card_frame = tkinter.Frame(card_frame, background='green')
    player_card_frame.grid(row=2, column=1, sticky='ew', rowspan=2)

    button_frame = tkinter.Frame(mainWindow)
    button_frame.grid(row=3, column=1, sticky='w', columnspan=3)

    dealer_button = tkinter.Button(button_frame, text='Dealer', command=deal_dealer)
    dealer_button.grid(row=0, column=0)

    player_button = tkinter.Button(button_frame, text='Player', command=deal_player)
    player_button.grid(row=0, column=1)

    # Load Cards
    cards = []
    load_images(cards)

    # Create a new deck of cards and shuffle them
    deck = list(cards)
    random.shuffle(deck)

    # Create the list to store dealer's and player's hands
    dealer_hand = []
    player_hand = []

    deal_player()
    dealer_hand.append(deal_card(dealer_card_frame))
    deal_player()

    mainWindow.mainloop()

LEAVE A REPLY

Please enter your comment!
Please enter your name here