Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Blackjack
#1
Hey guys! Been learning python through a private tutor and was given this code to fix/finish and I'm completely stuck here. For some reason I can't open up the .py file directly, and when I open it through the console its extremely buggy.

Task is to create a game of blackjack that follows the parameters of the original game, and can include "betting" for bonus points (player starts off with $100 and places a bet every round - if they win, its added to their total, if they lose its distracted, and once theyre out of money its game over

I have so many projects on my plate right now and only have a week to finish this assignment, any help would be greatly appreciated! Here's the code so far

import random

def card_deck():
    card_value = ['Ace','2','3','4','5','6','7','8','9','10','J','Q','K']
    card_type = ['Hearts','Spades','Clubs','Diamonds']
    deck = []
    for i in card_type:
        for j in card_value:
            deck.append(j + ' of ' + i)
    return deck

def card_value(card):
    if card[:1] in ('J','Q','K','1'):
        return int(10)
    elif card[:1] in ('2','3','4','5','6','7','8','9'):
        return int(card[:1])
    elif card[:1] == 'A':
        print ("\n"+ str(card))
        num = input("Do you want this to be 1 or 11?\n>")
        while num !='1' or num !='11':
            if num == '1':
                return int(1)
            elif num == '11':
                return int(11)
            else:
                num = input("Do you want this to be 1 or 11?\n>")


def new_card(deck):
    return deck[randint(0,len(deck)-1)]

def remove_card(deck,card):
    return deck.remove(card)


play_again = ''
while play_again != 'EXIT':
    #deck creation, card creation, card removal from deck, values and totals
    new_deck = card_deck()
    card1 = new_card(new_deck)
    remove_card(new_deck,card1)
    card2 = new_card(new_deck)
    remove_card(new_deck,card2)
    print ("\n\n\n\n" + card1 + " and " + card2) #doing this statement first allows for selection between 1 and 11
    valu1 = card_value(card1)
    valu2 = card_value(card2)
    total = valu1 + valu2
    print("with a total of " + str(total) )

    #dealer's hand
    dealer_card1 = new_card(new_deck)
    remove_card(new_deck,dealer_card1)
    dealer_card2 = new_card(new_deck)
    remove_card(new_deck,dealer_card2)
    dealer_value1 = card_value(dealer_card1)
    dealer_value2 = card_value(dealer_card1)
    dealer_total = dealer_value1 + dealer_value2
    print ('\nThe Dealer smiles as he looks at you and\n deals one card up and one card face down')
    print ("First a " + dealer_card1 + " and face down card.")

    if total == 21:
        print("Blackjack!")
    else:
        while total < 21: #not win or loss yet
            answer = input("Would you like to hit or stand?\n> ")
            if answer.lower() == 'hit':
                #more card creation, removal, and value added to total
                more_card = new_card(new_deck)
                remove_card(new_deck,more_card)
                more_value = card_value(more_card)
                total += int(more_value)
                print (more_card + " for a new total of " + str(total))
                if total > 21: #lose condition
                    print("You LOSE!")
                    play_again = input("Would you like to continue? EXIT to leave\n")
                elif total == 21: #winning condition
                    print("You WIN BIG WIN WOO WOO")
                    play_again = input("Would you like to continue? EXIT to leave\n")
                else:
                    continue
            elif answer.lower() == 'stand':
                print("The dealer nods and reveals his other card to be ")
                print("a " + dealer_card2 + " for a total of " + str(dealer_total))
                if dealer_total < 17:
                    print("The Dealer hits again.")
                    dealer_more = new_card(new_deck)
                    more_dealer_value = card_value(dealer_more)
                    print("The card is a " + str(dealer_more))
                    dealer_total += int(more_dealer_value)
                    if dealer_total > 21 and total <=21:
                        print("Dealer Bust! You win!")
                    elif dealer_total < 21 and dealer_total > total:
                        print("Dealer has " + str(dealer_total) + " You lose!")
                    else:
                        continue
                elif dealer_total == total:
                    print("Equal Deals, no winner")
                elif dealer_total < total:
                    print("You win!")
                else:
                    print("You Lose!")
                play_again = input("\nWould you like to continue? EXIT to leave\n")
                break
Reply
#2
What's the question? Ideally you can tell us what's wrong, instead of us debugging it for you.
Reply
#3
So when you try to run your code you are not seeing this?
Output:
Traceback (most recent call last): File "C:\Users\djhys\Documents\Python\Musings\junk.py", line 40, in <module> card1 = new_card(new_deck) File "C:\Users\djhys\Documents\Python\Musings\junk.py", line 30, in new_card return deck[randint(0,len(deck)-1)] NameError: name 'randint' is not defined
Reply
#4
(Apr-24-2020, 09:32 PM)deanhystad Wrote: So when you try to run your code you are not seeing this?
Output:
Traceback (most recent call last): File "C:\Users\djhys\Documents\Python\Musings\junk.py", line 40, in <module> card1 = new_card(new_deck) File "C:\Users\djhys\Documents\Python\Musings\junk.py", line 30, in new_card return deck[randint(0,len(deck)-1)] NameError: name 'randint' is not defined



When I'm trying to run my code the python interpreter is just crashing. it pulls up and then just exits itself :/ the one time I was able to get it running it kept giving me error messages and I have no idea what I did wrong
Reply
#5
How do you run your program?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Blackjack total from string HW ichistarr 5 6,782 Apr-27-2017, 05:55 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020