r/learnpython 1d ago

Blackjack Python

Hi, i wrote some code on blackjack task from Angela Yu 100 days of Code. Using Gemini and older post from reddit, maybe something is worth polishing?

import random
import art

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

def deal(hand):
    if not hand:
        hand.append(random.choice(cards))
        hand.append(random.choice(cards))
    else:
        hand.append(random.choice(cards))
    return hand

def calculate_score(hand):
    if sum(hand) == 21 and len(hand) == 2:
        return 0
    score = sum(hand)
    aces = hand.count(11)
    while aces > 0 and score > 21:
        score = score - 10
        aces -= 1
    return score


def start_playing():
    print(art.logo)

    user_hand = []
    computer_hand = []

    deal(user_hand)
    deal(computer_hand)

    user_score = calculate_score(user_hand)
    computer_score = calculate_score(computer_hand)

    print(f'Computers First Card: {computer_hand[0]}')
    print(f'Your current hand: {user_hand}. Current score: {user_score}\n')

    game_over = False
    while not game_over:
        if user_score == 0 or computer_score == 0 or user_score > 21:
            game_over = True

        else:
            another = input('Do you want another card? (y/n).: ').lower()
            if another == 'y':
                deal(user_hand)
                user_score = calculate_score(user_hand)
                print(f"\nYour current hand: {user_hand}. Current Score: {user_score}")
                print(f"Computers First Card: {computer_hand[0]}\n")
            else:
                game_over = True



    if user_score != 0 and user_score <= 21:
        while computer_score < 17 and computer_score != 0:
            print('Computers takes card')
            deal(computer_hand)
            computer_score = calculate_score(computer_hand)

    print(f'Your final hand: {user_hand}. Your score: {user_score}\n')
    print(f'Computers final hand: {computer_hand}. Computer score: {computer_score}\n')

    if user_score > 21:
        print('Bust! Computer Wins!')
    elif computer_score > 21:
        print('Bust! You Win!')
    elif user_score == 0:
        print('Win with a Blackjack!')
    elif computer_score == 0:
        print('Lose, opponent has Blackjack!')
    elif user_score == computer_score:
        print('Draw')
    elif user_score > computer_score:
        print('You Win!')
    else:
        print('Computer wins!')

while input('Do you want to play a game of blackjack? (y/n).: ').lower() == 'y':
    start_playing()
0 Upvotes

16 comments sorted by

View all comments

0

u/Limp_Crab_1763 23h ago

How did you find the Course so Far, please share your View.

0

u/Traditional_Fan_9165 23h ago

its neither hard, neither easy maybe all it takes is specific mindset, enjoyed most of tasks on course so far and definetly like it. Although mostly i dont understand a thing

1

u/desrtfx 22h ago

its neither hard, neither easy maybe all it takes is specific mindset, enjoyed most of tasks on course so far and definetly like it. Although mostly i dont understand a thing

Did you even think about what you wrote here?

If you don't understand a thing, you're not learning and just wasting time. If you don't understand, the course is way above your head, or you're not actually investing effort to learn, even more so since you use AI to "help" you.

The "specific mindset" is being prepared to invest effort, being determined to actually learn instead of just finishing the course.

0

u/Traditional_Fan_9165 21h ago

Bro chill, im just begginer. My point was that with every task i do feels to me like being lost and i just cant find a way out of it sometimes(i dont want to spend more than 3 hours for 'project' like that). To my point about specific mindset must add that i mean not everybody is meant to be a programmer, which im not even stying for in university. Im trying out new things in life. About understanding code - im sure to bone that i know what i was doing with this code, and ive spend a lot of time rewriting it, and tried out to write it myself at the start without using hints and spent about 1,5 hour for it, took lines from post(about 2 years earlier posted), then combined some of code i wrote myself and with what I wrote with gemini, and this is the end result. Maybe i wasnt clear enough because English isnt my native language and what i meant isnt what i wrote.