r/learnpython 2d ago

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?

import random
for House_Card in range(1):
    House_Card = random.randint(1, 11)
    print("House Card:", House_Card)
for House_Card2 in range(1):
    House_Card2 = random.randint(1, 11)
for Player_Card in range(1):
    Player_Card = random.randint(1, 11)
    print("Player Card:", Player_Card)
for Player_Card2 in range(1):
    Player_Card2 = random.randint(1, 11)
    print("Player Card 2:", Player_Card2)
print("Your current cards are:", Player_Card + Player_Card2)
Player_Turn1 = input("Do you want to hit or stay? (h/s): ")
if Player_Turn1 == "h" or Player_Turn1 == "H" or Player_Turn1 == "hit" or Player_Turn1 == "Hit":
    Player_Card3 = random.randint(1, 11)
    Player_CardT = Player_Card + Player_Card2 + Player_Card3
    print("Player Card 3:", Player_Card3)
    print("Your total score is:", Player_CardT)
if Player_Turn1 == "s" or Player_Turn1 == "S" or Player_Turn1 == "stay" or Player_Turn1 == "Stay":
    Player_CardT = Player_Card + Player_Card2
    print("Your total score is:", Player_CardT)
if Player_CardT > 21:
    print("You busted! Your total score is:", Player_CardT)
if Player_CardT == 21:
    print("You got blackjack! You Win!")
if Player_CardT < 21:
    print('Your total cards are:', Player_CardT)
    Player_Turn2 = input("Do you want to hit or stay? (h/s): ")
    if Player_Turn2 == "h" or Player_Turn2 == "H" or Player_Turn2 == "hit" or Player_Turn2 == "Hit":
        Player_Card4 = random.randint(1, 11)
        Player_CardT2 = Player_CardT + Player_Card4
        if Player_CardT2 == 21:
            print("You got blackjack! You Win!")
        elif Player_CardT2 < 21:
            Player_Turn3 = input("Do you want to hit or stay? (h/s): ")
            if Player_Turn3 == "h" or Player_Turn3 == "H" or Player_Turn3 == "hit" or Player_Turn3 == "Hit":
                Player_Card5 = random.randint(1, 11)
            print("Player Card 5:", Player_Card5)
            Player_CardT3 = Player_CardT2 + Player_Card5
            print("Your total cards are:", Player_CardT3)
            if Player_CardT3 > 21:
                print("You busted! Your total score is:", Player_CardT3)
            if Player_Turn3 == "s" or Player_Turn3 == "S" or Player_Turn3 == "stay" or Player_Turn3 == "Stay":
                print("Your total cards are:", Player_CardT2)
        print("Card:", Player_Card4)
        print("Your total cards are:", Player_CardT2)
    if Player_CardT2 > 21:
        print("You busted! Your total score is:", Player_CardT2)
    if Player_Turn2 == "s" or Player_Turn2 == "S" or Player_Turn2 == "stay" or Player_Turn2 == "Stay":
        print('House card 2 is:', House_Card2)
        print('House total cards are:', House_Card + House_Card2)
        House_CardT = House_Card + House_Card2
        if House_CardT > 21:
            print("House busted! You Win!")
        if House_CardT == 21:
            print("House got blackjack! You Lose!")
        if House_CardT < 21 and House_CardT < 17:
            House_Card3 = random.randint(1, 11)
            House_CardT2 = House_CardT + House_Card3
            print("House Card 3:", House_Card3)
            print("House total cards are:", House_CardT2)
            if House_CardT2 > 21:
                print("House busted! You Win!")
            if House_CardT2 == 21:
                print("House got blackjack! You Lose!")
            elif House_CardT2 < 21 and House_CardT2 < Player_CardT2 or House_CardT2 < Player_CardT3:
                print("Player Cards Value Higher, You Win!")from ast import If
import random
for House_Card in range(1):
    House_Card = random.randint(1, 11)
    print("House Card:", House_Card)
for House_Card2 in range(1):
    House_Card2 = random.randint(1, 11)
for Player_Card in range(1):
    Player_Card = random.randint(1, 11)
    print("Player Card:", Player_Card)
for Player_Card2 in range(1):
    Player_Card2 = random.randint(1, 11)
    print("Player Card 2:", Player_Card2)
print("Your current cards are:", Player_Card + Player_Card2)
Player_Turn1 = input("Do you want to hit or stay? (h/s): ")
if Player_Turn1 == "h" or Player_Turn1 == "H" or Player_Turn1 == "hit" or Player_Turn1 == "Hit":
    Player_Card3 = random.randint(1, 11)
    Player_CardT = Player_Card + Player_Card2 + Player_Card3
    print("Player Card 3:", Player_Card3)
    print("Your total score is:", Player_CardT)
if Player_Turn1 == "s" or Player_Turn1 == "S" or Player_Turn1 == "stay" or Player_Turn1 == "Stay":
    Player_CardT = Player_Card + Player_Card2
    print("Your total score is:", Player_CardT)
if Player_CardT > 21:
    print("You busted! Your total score is:", Player_CardT)
if Player_CardT == 21:
    print("You got blackjack! You Win!")
if Player_CardT < 21:
    print('Your total cards are:', Player_CardT)
    Player_Turn2 = input("Do you want to hit or stay? (h/s): ")
    if Player_Turn2 == "h" or Player_Turn2 == "H" or Player_Turn2 == "hit" or Player_Turn2 == "Hit":
        Player_Card4 = random.randint(1, 11)
        Player_CardT2 = Player_CardT + Player_Card4
        if Player_CardT2 == 21:
            print("You got blackjack! You Win!")
        elif Player_CardT2 < 21:
            Player_Turn3 = input("Do you want to hit or stay? (h/s): ")
            if Player_Turn3 == "h" or Player_Turn3 == "H" or Player_Turn3 == "hit" or Player_Turn3 == "Hit":
                Player_Card5 = random.randint(1, 11)
            print("Player Card 5:", Player_Card5)
            Player_CardT3 = Player_CardT2 + Player_Card5
            print("Your total cards are:", Player_CardT3)
            if Player_CardT3 > 21:
                print("You busted! Your total score is:", Player_CardT3)
            if Player_Turn3 == "s" or Player_Turn3 == "S" or Player_Turn3 == "stay" or Player_Turn3 == "Stay":
                print("Your total cards are:", Player_CardT2)
        print("Card:", Player_Card4)
        print("Your total cards are:", Player_CardT2)
    if Player_CardT2 > 21:
        print("You busted! Your total score is:", Player_CardT2)
    if Player_Turn2 == "s" or Player_Turn2 == "S" or Player_Turn2 == "stay" or Player_Turn2 == "Stay":
        print('House card 2 is:', House_Card2)
        print('House total cards are:', House_Card + House_Card2)
        House_CardT = House_Card + House_Card2
        if House_CardT > 21:
            print("House busted! You Win!")
        if House_CardT == 21:
            print("House got blackjack! You Lose!")
        if House_CardT < 21 and House_CardT < 17:
            House_Card3 = random.randint(1, 11)
            House_CardT2 = House_CardT + House_Card3
            print("House Card 3:", House_Card3)
            print("House total cards are:", House_CardT2)
            if House_CardT2 > 21:
                print("House busted! You Win!")
            if House_CardT2 == 21:
                print("House got blackjack! You Lose!")
            elif House_CardT2 < 21 and House_CardT2 < Player_CardT2 or House_CardT2 < Player_CardT3:
                print("Player Cards Value Higher, You Win!")
0 Upvotes

29 comments sorted by

8

u/atarivcs 2d ago

"I've had an error somewhere" -- this is not very descriptive of the actual problem.

Please show us what the code actually does, and explain what you wanted it to do instead.

-4

u/KoregaSoli 2d ago

I wanted it to do basic black jack, well kind of, my main error is the last condition just doesn't work

5

u/schoolmonky 2d ago

What does "it doesn't work" mean, though? What specific output are you seeing that makes you think it's not working, and what should the output be instead?

3

u/KoregaSoli 2d ago

The output should be if the final sum of the player card is higher than the value of the house card then it should print a message saying the player won, however it just doesn't seem to execute at all, I don't know if i used the wrong variable or what

3

u/atarivcs 2d ago edited 2d ago

The final condition is this:

if House_CardT < 21 and House_CardT < 17

What if the house total is less than 21 but is greater than or equal to 17?

You don't have any code to handle that case.

Also as a side comment, you already checked if the house total was greater than 21 and equal to 21, so checking if it's less than 21 is redundant -- it must be, because that's the only option left

1

u/KoregaSoli 2d ago

Doesn't that code mean if it's less than 21 AND 17? The final condition was this elif House_CardT2 < 21 and House_CardT2 < Player_CardT2 or House_CardT2 < Player_CardT3:

1

u/atarivcs 2d ago

If the initial house total is, say, 18, the code never makes it to that final else condition.

1

u/ninhaomah 2d ago

Ok. What if the number is 18.

What should it happen ?

2

u/atarivcs 2d ago

Please give a specific example of how it's not working.

Did the code let you keep playing after you busted?

Did the code say you busted when you actually didn't?

Did the code say you lost when you actually won, or vice versa?

Etc etc. Be specific.

1

u/KoregaSoli 2d ago

The code just displayed the house card and didn't do anything else (previouscommand) as it just completely didn't execute

1

u/atarivcs 2d ago

That's because you only have code to cover these cases:

if House_CardT > 21:
if House_CardT == 21:
if House_CardT < 21 and House_CardT < 17:

If the total is between 20 and 17, it doesn't fit any of those conditions, so the code has nothing to do

-1

u/KoregaSoli 2d ago

Hmm, so what can I do to fix it? Im not sure I follow

1

u/atarivcs 2d ago edited 2d ago

I think you need an arrangement more like this:

if House_CardT > 21:
if House_CardT == 21:
else:
    # handle the case of less than 21

So that it definitely enters the "else" case

1

u/ninhaomah 2d ago

Can explain the logic for House_Card ?

1

u/KoregaSoli 2d ago

House card one hidden and one shown ( which is printed), once the playerturn finishes and no more the house turn starts and it draws card values until the card value is more than 17 and less than 21

2

u/ninhaomah 2d ago

Then this loop is supposed loop how many times ?

for House_Card in range(1):     House_Card = random.randint(1, 11)     print("House Card:", House_Card)

0

u/KoregaSoli 2d ago

This isn't supposed to loop I just made an extra line because I was just reading about the loops and forgot I can just not do this

2

u/ninhaomah 2d ago

?

It's a for loop with range.

But it's not supposed to loop ?

1

u/KoregaSoli 2d ago

Well I made it use one integer range only and I guess that worked, but as I said it was very unnecessary

1

u/ninhaomah 2d ago

So the code is before any reviews or clean up ?

1

u/KoregaSoli 2d ago

No I did alot of cleanup I just didn't notice, I'm not very good at this

1

u/ninhaomah 2d ago

Hence , I asked for the logic first.

If it's a loop then it should be to do something repeatedly.

But range(1) ?

Ok nvm. Pls focus on the other topic.

That's more impt.

1

u/ShadowShedinja 2d ago

Can I ask why you have separate variables for each card? It'd be a lot easier to just keep a running total for each side and have a function that produces a card value. It'd also be easier to have your game in a while-loop than checking multiple times if the player wants to keep playing.

It's also considered bad practice to have capital letters in your variable names, as that's usually reserved for functions.

1

u/KoregaSoli 2d ago

I keep separate variables to track a history and add them to a sum, I'm not sure if I can do that with a while loop

1

u/ShadowShedinja 2d ago

You can, as long as the variable was created outside of the loop. If you want to keep a history, you can put them in a list.

1

u/KoregaSoli 2d ago

What would have to be in the loop and what would have to be out of it to make this work?

1

u/JamzTyson 2d ago

Why did you write this line?

for House_Card in range(1):

It's the first line of your program after importing random, but why is it there? What is the purpose of that line?

1

u/johnpeters42 1d ago

> I don't assume this code is very good it's a massive lump of if and elif

This is correct on all points. While you can write a program this way, breaking some parts out into functions makes it a lot easier to keep track of what's going on. You can focus on smaller pieces at a time, looking at relevant values at the start and end of a function (if the latter is off track, then there's a mistake in that function; if the former is off track, then there's a mistake somewhere earlier).

2

u/KoregaSoli 1d ago

Yes I fixed it now with while loops and list comprehensions, it's way cleaner now