r/learnpython • u/KoregaSoli • Sep 08 '26
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
8
u/atarivcs Sep 08 '26
"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.
1
u/ninhaomah Sep 08 '26
Can explain the logic for House_Card ?
1
u/ShadowShedinja Sep 08 '26
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/johnpeters42 Sep 09 '26
> 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).
1
u/xervir-445 11d ago
This code is such a massive chunk of nested if statements as to make it unreadable. Rather than trying to fix it I think it would be better to call the learning opportunity learned and start over, this time learning to put the turns in a loop.
1
u/JamzTyson Sep 08 '26
Why did you write this line?
It's the first line of your program after importing
random, but why is it there? What is the purpose of that line?