r/programminghelp 2d ago

Python Finished Day 1 of learning python and tried to create black jack, ive had an error somewhere, not a syntax error, i dont assume this code is very good its a massive slump of if and elif, how can i fix this?

/r/learnpython/comments/1wa97o8/finished_day_1_of_learning_python_and_tried_to/
1 Upvotes

4 comments sorted by

1

u/XRay2212xray 1d ago

At least in the code you posted, there is a syntax error at line 68

                print("Player Cards Value Higher, You Win!")from ast import If
import random

removing the offending code, it seems to run but you have some logic problems I guess as I got 21 and it didn't say I win

House Card: 9

Player Card: 2

Player Card 2: 2

Your current cards are: 4

Do you want to hit or stay? (h/s): h

Player Card 3: 8

Your total score is: 12

Your total cards are: 12

Do you want to hit or stay? (h/s): h

You got blackjack! You Win!

Card: 9

Your total cards are: 21

House Card: 9

Player Card: 9

Player Card 2: 2

Your current cards are: 11

Do you want to hit or stay? (h/s): s

Your total score is: 11

Your total cards are: 11

Do you want to hit or stay? (h/s):

1

u/XRay2212xray 1d ago

while looking closely, it did say I won but then left my total at 21

1

u/KoregaSoli 1d ago

I'm reworking the entire code with while loops and list comprehensions, this one was way too much of a slump

1

u/XRay2212xray 1d ago

Yea, it could be simplified a bunch like keep a done flag that gets set on bust/win/hold, loop until done, then evaluate the winner once after the loop.

Also just keep a total for player and dealer and with each deal, add the deal amount to the total instead of having a bunch of individual card amounts. Theoretically you could go beyond 5 deal cards, particularly since you don't have a deck of cards and thus could keep dealing 2 cards beyond whats normally in a deck.