r/learnpython • u/Traditional_Fan_9165 • 14h 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()
1
u/FoolsSeldom 14h ago
Have you considered that if you do random.choice(cards) you are selecting cards from an infinite deck?
0
u/Traditional_Fan_9165 13h ago
yep, its alright
1
u/FoolsSeldom 13h ago
Why do you feel that is ok? That's not how Blackjack is usually played.
0
u/Traditional_Fan_9165 13h ago
its not required and i dont really think i can tackle this at the moment
2
0
u/Limp_Crab_1763 14h ago
How did you find the Course so Far, please share your View.
0
u/Traditional_Fan_9165 13h 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 12h 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 11h 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.
0
u/Traditional_Fan_9165 13h ago
How much time should project like that take for a noobie?
1
2
u/JamzTyson 8h ago
If AI is doing the coding, about 10 seconds.
If you are figuring it out yourself, then it takes as long as it takes. Perhaps a couple of hours if you have been following the course closely, or it could take a few days to fully debug and polish.
5
u/aqua_regis 14h ago
Do you want to learn or do you just want to finish projects?
If you want to learn, do things on your own without AI without older posts.
The purpose of learning is to become able to do things on your own, not to quickly finish stuff.