#πŸ”’ Betting system in blackjack - just need help modifying a few lines

140 messages Β· Page 1 of 1 (latest)

topaz gyro
#

Hi, i need help with my betting system. When i win, the money isnt added back into the account

wheat summitBOT
#

@topaz gyro

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wheat summitBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

topaz gyro
#

Will do

quaint adder
#

true

quaint adder
#

wow that long

topaz gyro
#

Issue i have is when i win or dealer busts and i win my bet money, the balance doesnt update to register it

candid hollow
#

I'm not really familiar with pygame, but the first thing I notice is that it's very odd having your Play class nested inside your main function

topaz gyro
candid hollow
#

I can't think of a single reason why you'd need to nest a class

quaint adder
#

can you show about what line is the balance adding code is

topaz gyro
candid hollow
topaz gyro
#
        TextSurf, TextRect = text_objects(text, textfont)
        TextSurf = font.render(text, True, color)
        TextRect.center = (x, y)
        gameDisplay.fill(background_color, pygame.Rect(x-100, y-30, 200, 50))  # Clear the old text
        gameDisplay.blit(TextSurf, TextRect)```
#
            game_texts(f"Current Bet: {play_blackjack.current_bet}", 100, 370, white)

            # Display the current bet and balance
            game_texts(f"Current Bet: {play_blackjack.current_bet}", 100, 370, white)
            game_balance(f"Balance: {play_blackjack.current_balance}", 100, 330, white)```
candid hollow
#

which line do you think is modifying the balance?

topaz gyro
#

and then it reprints

game_balance(f"Balance: {self.balance}", 100, 330, white)

candid hollow
#

and then you call self.play_or_exit()

topaz gyro
#

Yeah

candid hollow
#
def play_or_exit(self):
    time.sleep(1)
    self.reset_balance()
#

and there you self.reset_balance()

topaz gyro
#
            self.balance = self.balance
            game_balance(f"Balance: {self.balance}", 100, 330, white)```
candid hollow
#
self.balance = self.balance
#

what is the point of this?

topaz gyro
#

i dont know

#

sorry im new to python

#

this is my first game i made

quaint adder
#

new and 500 line of code

#

am still trying to fine that part u send me xd

topaz gyro
#

then edited

quaint adder
#

am to dumb for this

candid hollow
#

Yeah, this is quite a big undertaking if you're new

topaz gyro
quaint adder
#

true

topaz gyro
#

but i fix

quaint adder
#

using class when you new

topaz gyro
#

now it just balance

candid hollow
#
self.player = Hand()
#

you have this, but I'm not seeing a Hand class anywhere

topaz gyro
#

Another file

#

hold on

candid hollow
#

can you show that?

topaz gyro
#
from constants import *

class Deck:
    def __init__(self):
        self.cards = []
        self.build()

    def build(self):
        for value in RANKS:
            for suit in SUITS:
                self.cards.append((value, suit))
  
    def shuffle(self):
        random.shuffle(self.cards)
        

    def deal(self):
        if len(self.cards) > 1:
            return self.cards.pop()
            
class Hand(Deck):
    def __init__(self):
        self.cards = []
        self.card_img = []
        self.value = 0 

    def add_card(self, card):
        self.cards.append(card)

    def calc_hand(self): #grouping cards + calculating values
        self.value = 0
        first_card_index = [a_card[0] for a_card in self.cards]
        non_aces = [c for c in first_card_index if c != 'A']
        aces = [c for c in first_card_index if c == 'A']

        for card in non_aces:
            if card in 'JQK':
                self.value += 10 #picture cards JQK worth 10
            else:
                self.value += int(card) #other cards have face value


        for card in aces: #ace is worth 11/1 depending on hand value
            if self.value <= 10:
                self.value += 11
            else:
                self.value += 1

#displaying cards on screen
    def display_cards(self):
        for card in self.cards:
            cards = "".join((card[0], card[1]))
            if cards not in self.card_img:
                self.card_img.append(cards)
candid hollow
#

ahh ok so it's separate logic from the balance

topaz gyro
#

Yeah

#

more about the card values

#

and deck shuffling building

#

all the balance is just in the main file

candid hollow
#

hmm nothing else really seems out of place

#

there could be a disconnect from the actual variable data and how you're updating it in pygame

#

I'd try and use some print statements and print out the balance to confirm its value

topaz gyro
#

alr

#

<function main.<locals>.game_balance at 0x0000024BFB2E84C0>

#

it said that

#

when i tried printing the balance after it updating

candid hollow
#

can you show how you tried to do that?

quaint adder
#

there are so much repeated code in here?

topaz gyro
#

OH

#

WAIT

#

OOPS

candid hollow
#

self.balance holds your balance, you're printing the wrong thing

candid hollow
quaint adder
#

did u like coppy the code from tut and forgot?

quaint adder
topaz gyro
#

i dont think anythings repeated in the main():?

quaint adder
#

you defind the back function twice

topaz gyro
#

oh i did too

#

sorry i did that at like 1 htis morning

#

i was tryna sort out the diff menu screens

quaint adder
#

also i cant see where did you use that func

#

i suppose that not related to your queston tho

topaz gyro
#

line 440

#

button("Back", 340, 450, 150, 35, white, dark_slat, back)

quaint adder
#

uh

#

the back() func

#

ok

lean elm
#

Hi silly! πŸ‘‹ you still grinding on this issue?

topaz gyro
#

Yessir

topaz gyro
#

could you help please?

#

if you scrollup youll see the code

#

whats happening is that when im winning, the balance isnt updating with the earnt money

candid hollow
topaz gyro
#

I bet 100 and had a balance of 550 remaining

#

I won and it listed my balance as 850 in the print (not updated on screen)

#

then it glitched and said i Tied and reset my balance + bet to 550 and 0

#

And i just clicked -50 to subtract from my already 0 current bet and it allowed me to add it back to my account...

#

meaning i earnt 300..

candid hollow
#

is it the print showing you this, or the game balance text in the game?

candid hollow
#

use print

#

only listen right now to what print says

#

because it's possible you simply aren't updating your game display correctly

topaz gyro
#

Okay

#

i bet 100 and have balance of 900

#

Tied

#

printed my new balance at 1200

#

but display reads

#

and then when i click +50 to current bet i get this:

candid hollow
#

completely ignore your display

#

we're just focusing on the truth of the value

#

once you know it's working 100% correctly with printing

#

then you can focus on making sure the game display updates along with it

topaz gyro
#

The true value is working

#

More or less

#

I bet 100, have a balance of 900 remainder and it sets my total at 1200

#

meaning it tripled my bet although the code asks it to double

#
                    game_finish("You Won!", 500, 40, white)
                    self.balance += self.bet * 2  # Add the win amount to the balance
                    print(self.balance )
                    game_balance(f"Balance: {self.balance}", 100, 330, white)
                    self.bet = 0  # current bet
                    self.current_bet = 0  # current bet display
                    time.sleep(3)
                    self.play_or_exit()```
candid hollow
#

what's the difference between self.bet and self.current_bet?

topaz gyro
#

bet is the adding subtracting from current bet

#

which is like the total

#

displayed

#
            if self.bet + 50 > self.balance:
                print("Insufficient balance")
                return
            self.bet += 50
            self.current_bet = self.bet
            self.current_balance = self.balance - self.bet

        def sub_bet(self):
            if self.bet == 0:
                return  # Do nothing if the current bet is already at 0
            if self.bet - 50 < 0:
                print("Minimum bet is 0")
            self.bet -= 50
            self.current_bet = self.bet
            self.current_balance = self.balance - self.bet
            return```
candid hollow
#

you seem to just always be setting current_bet to bet

#

I don't understand why you need both

topaz gyro
#

I probably dont

candid hollow
#

try and eliminate any redundancy. If something isn't doing anything, you can probably remove it

#

the less stuff you have to keep track of, the easier it is to debug (and write new code)

topaz gyro
#

Deleted it

#

same error

#

the print text is true

#

the balance text is old

#

but when i click +50/-50 it makes it updated?

topaz gyro
candid hollow
#

keep using prints to track down when things go awry

topaz gyro
#

when i win/tie/lose

#

thats when its bad

wheat summitBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.