Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

trying to make blackjack game as project

import random

def hitme(): card = random.randint(1, 10)

def yourhand(): blackjack = 21 new_card1 = random.randint(1, 10) new_card2 = random.randint(1, 10) card = random.randint(1, 10) hand = [new_card1, new_card2, card]

while True:

    print("Your two cards are: {} and {}".format(new_card1, new_card2))

    if len(hand) == 21:
        print("BlackJack! You Win!")
        break
    elif len(hand) < 21:
        hit = input("Another card?, Y/N?").lower()    
        if hit == "Y":
            return card + hand
    else:
        print("You lose!")

while True: yourhand()

This is my code ive written for practice. It keeps looping on the "Hit me" part and keeps saying "Your two cards are: 6 and 2." Can someone help me out on how to add the hit to the hand. Thanks so much

1 Answer

Interesting project. Here is what I am thinking:

  1. The game play can let people split and have multiple cards. You might want to make the hand a list or set maybe. Then you can use things like sum(hand) to check its value.

  2. In blackjack there are a lot of ways to get the 10. And also the Ace that can be both. But these are more exotic conditions that you might want to handle later. However, it might be useful to think about the representation of a card with an object perhaps. Just thinking out loud here.

  3. The game is versus someone. So, you want to deal cards for an adversary too and check against their cards for declaring winners.

Hope I contributed. Good luck!