Home > database >  Blackjack Capstone Project from 100 Days of Code with Dr. Angela Yu
Blackjack Capstone Project from 100 Days of Code with Dr. Angela Yu

Time:11-21

This question is in relation to Dr Angela Yu's 11th day of Python tutorials. I am not able to execute the code I typed in. The code is typed in replit. Where am I making mistakes? This code is supposed to play the game of Blackjack.

import random
from replit import clear
from art import logo

def draw_card():
  cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
  return random.choice(cards)

def calculate_score(cards):
  if sum(cards) == 21 and len(cards) == 2:
    return 0
  if sum(cards) > 21 and 11 in cards:
    cards.remove(11)
    cards.append(1)
  return sum(cards)

def compare(user_score, computer_score):
  if user_score > 21 and computer_score > 21:
    print("You went over 21. You lost")
  elif computer_score == 0:
    print("You lost. Computer has blackjack")
  elif user_score == 0:
    print("You won with a blackjack.")
  elif user_score == computer_score:
    print("Draw")
  elif user_score > 21:
    print("You lost")
  elif computer_score > 21:
    print("you won")
  elif user_score > computer_score:
    print("You won.")
  else:
    print("Computer won")

def play_game():
  print (logo)
  user_cards = []
  computer_cards = []
  for number in range(2):
    user_cards.append(draw_card())
    computer_cards.append(draw_card())
  
  game_end = False
  
  while not game_end:
  
    user_score = calculate_score(user_cards)
    computer_score = calculate_score(computer_cards)
    
    print(f"   Your cards: {user_cards}, your score: {user_score}.")
    print(f"   Computer's first card: {computer_cards[0]}")
    
    get_card = input("Type 'y' to get another card, type 'n' to pass: ")
    if get_card == "y".lower():
      user_cards.append(draw_card())
  
    else:
      game_end = True
      while computer_score < 17:
        computer_cards.append(draw_card)
  
  print(f"   Your final hand: {user_cards}, final score: {user_score}")
  print(f"   Computer's final hand: {computer_cards}, final score: {computer_score}")
  print(compare(user_score, computer_score))


play = input("Do you want to play a game of blackjack. Type y or n: ").lower()
while play == "y":
  clear()
  play_game()
  




I am not able to debug this code in thonny due to some functions I can only find in replit.

CodePudding user response:

You never recompute computer_score, so computer_score < 17 will stay True forever.

CodePudding user response:

There are bugs in your code.Here are the solutions. Do this:

#1
from art import logo   #delete this line at top of your code
from art import *      #add this line instead

#2
# Delete line number 37 which says "print(logo)"
tprint("Text") #Add this line instead

#3
#The main bug is that the program gets stuck at while loop in around lineNO 62 where it says "while computer_score < 17:"
#I could solve it for you but i don't know the game, so do something there.
#My suggestion: use if statement instead of while loop.
  • Related