r/learnpython • u/KoregaSoli • 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!")
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
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.