It just skips over the fact that I put no when it asks I put no, and when I do put no, it just re-asks me if I want to hit.
I've tried making a new while statement for when you don't want to hit and it still didn't work.
import time
from random import randint
cardg = 0
cardg1 = 0
yeet = 1
while cardg < 21:
hit = input("Do you want to hit? (Yes or No): ")
if hit == "yes" or hit == "Yes":
num = randint(1, 10)
num2 = randint(1, 4)
card = num
card2 = num2
if num == 1:
card1 = "Ace"
elif num == 11:
card1 = "Jack"
elif num == 12:
card1 = "Queen"
elif num == 13:
card1 = "King"
if num2 == 1:
card2 = "Hearts"
if num2 == 2:
card2 = "Diamonds"
if num2 == 3:
card2 = "Spades"
if num2 == 4:
card2 = "Clubs"
if num == 1:
card = input("Would you like your ace to be a 1 or 11?\nAnswer: ")
print("Ace of " + str(card2))
cardg += int(card)
time.sleep(1)
if cardg < 21:
print(str(card) + " of " + str(card2))
elif cardg == 21:
time.sleep(1)
print(str(card) + " of " + str(card2))
print("Blackjack, you win!")
yeet = 0
elif cardg > 21:
print(str(card) + " of " + str(card2))
print("Busted, you lose.")
yeet = 0
else:
while cardg1 < 21 and cardg1 > 18:
num = randint(1, 11)
cardg1 += int(num)
num2 = randint(1, 4)
card = num
card2 = num2
if num == 1:
card1 = "Ace"
elif num == 11:
card1 = "Jack"
elif num == 12:
card1 = "Queen"
elif num == 13:
card1 = "King"
if num2 == 1:
card2 = "Hearts"
if num2 == 2:
card2 = "Diamonds"
if num2 == 3:
card2 = "Spades"
if num2 == 4:
card2 = "Clubs"
cardg1 += int(card)
time.sleep(1)
if cardg1 < 21 and cardg1 > 18:
print(str(card) + " of " + str(card2))
print("You have " + str(cardg) + " points, dealer has " + str(cardg1) + " points.")
if cardg > cardg1:
print("Dealer has won!")
yeet = 0
else:
print("You have won!")
yeet = 0
elif cardg1 == 21:
time.sleep(1)
print(str(card) + " of " + str(card2))
print("Dealer has blackjack, you lose!")
yeet = 0
elif cardg1 > 21:
print(str(card) + " of " + str(card2))
print("Dealer busted, you win!")
yeet = 0
When ran, this is this output. The getting 21 and busting code works.
Do you want to hit? (Yes or No): yes
3 of Clubs
Do you want to hit? (Yes or No): yes
9 of Hearts
Do you want to hit? (Yes or No): no
Do you want to hit? (Yes or No): n
Do you want to hit? (Yes or No): no
Do you want to hit? (Yes or No):
cardg1 is initialized to 0. Therefore, the while condition cardg1 < 21 and cardg1 > 18 fails, as 0 is not in between 18 and 21, hence the else block does nothing.
Related
I'm making a blackjack game in python, but the .pop(0) method I'm using to get rid of cards comes up with an attribute error, does anyone know why?
for i in range(2):
drawnCard = newDeck.pop(0)
playerList.append(drawnCard)
if (drawnCard[:1] == "J" or drawnCard[:1] == "Q" or drawnCard[:1] == "K"):
playerSum += 10
elif (drawnCard[:1] == "A"):
playerSum += 11
elif ((drawnCard[:2]) == "10"):
playerSum += 10
else:
playerSum += int(drawnCard[:1])
here's the full code
from random import shuffle
deck = ["A♠", "2♠", "3♠", "4♠", "5♠", "6♠", "7♠", "8♠", "9♠", "10♠", "J♠", "Q♠", "K♠", "A♥", "2♥", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥", "10♥", "J♥", "Q♥", "K♥", "A♣", "2♣", "3♣", "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "10♣", "J♣", "Q♣", "K♣", "A♦", "2♦", "3♦", "4♦", "5♦", "6♦", "7♦", "8♦", "9♦", "10♦", "J♦", "Q♦", "K♦"]
newDeck = shuffle(deck)
playerList = []
playerSum = 0
dealerList = []
dealerSum = 0
for i in range(2):
drawnCard = newDeck.pop(0)
playerList.append(drawnCard)
if (drawnCard[:1] == "J" or drawnCard[:1] == "Q" or drawnCard[:1] == "K"):
playerSum += 10
elif (drawnCard[:1] == "A"):
playerSum += 11
elif ((drawnCard[:2]) == "10"):
playerSum += 10
else:
playerSum += int(drawnCard[:1])
if len(playerList) == 2:
if playerSum == 21:
if dealerSum != 21:
print ("Players Hand: ")
print (playerList)
print()
print ("Total: ")
print (playerSum)
print("Congrats, you got a blackjack!!!")
exit("Won Game")
if dealerSum == 21:
print ("Players Hand: ")
print (playerList)
print()
print ("Total: ")
print (playerSum)
print("You both got a blackjack, it's a tie!")
exit("Game Tied!")
playing = True
while(playing):
print ("Players Hand: ")
print (playerList)
print()
print ("Total: ")
print (playerSum)
user = input("Would you like to hit or stay? (h/s)\n")
if (user == "h"):
drawnCard = newDeck.pop(0)
playerList.append(drawnCard)
if (drawnCard[:1] == "J" or drawnCard[:1] == "Q" or drawnCard[:1] == "K"):
playerSum += 10
elif (drawnCard[:1] == "A"):
playerSum += 11
elif (drawnCard[:2] == "10"):
playerSum += 10
else:
playerSum += int(drawnCard[:1])
print ("Players Hand: ")
print (playerList)
print()
print ("Total: ")
print (playerSum)
print("---------------------------------------------------------------------------------------------------------------------------------------------------------------------")
if playerSum > 21:
if 'A' in playerList:
playerSum -= 10
else:
print ("Players Hand: ")
print (playerList)
print()
print ("Total: ")
print (playerSum)
print("Wow you suck at blackjack ._.")
exit("Game Lost!")
elif (user == "s"):
print("---------------------------------------------------------------------------------------------------------------------------------------------------------------------")
playing = False
for i in range(2):
drawnCard = newDeck.pop(0)
dealerList.append(drawnCard)
if (drawnCard[:1] == "J" or drawnCard[:1] == "Q" or drawnCard[:1] == "K"):
dealerSum += 10
elif (drawnCard[:1] == "A"):
dealerSum += 11
elif (drawnCard[:2] == "10"):
dealerSum += 10
else:
dealerSum += int(drawnCard[:1])
playing = True
while (playing):
if dealerSum >= 17:
playing = False
else:
drawnCard = newDeck.pop(0)
dealerList.append(drawnCard)
if (drawnCard[:1] == "J" or drawnCard[:1] == "Q" or drawnCard[:1] == "K"):
dealerSum += 10
elif (drawnCard[:1] == "A"):
dealerSum += 11
elif ((drawnCard[:2]) == "10"):
dealerSum += 10
else:
dealerSum += int(drawnCard[:1])
print ("Dealers Hand: ")
print (dealerList)
print()
print ("Total: ")
print (dealerSum)
print("---------------------------------------------------------------------------------------------------------------------------------------------------------------------")
if dealerSum > 21:
if "A" in dealerList:
dealerSum -= 10
else:
print("Players hand: ")
print(playerList)
print()
print("Total: ")
print(playerSum)
print("Dealers hand: ")
print(dealerList)
print()
print("Total: ")
print(dealerSum)
print("The dealer busted! Congrats!")
exit("Won Game! ")
if playing == False:
if (dealerSum > playerSum):
print("---------------------------------------------------------------------------------------------------------------------------------------------------------------------")
print("Final results: ")
print("Players hand: ")
print(playerList)
print()
print("Total: ")
print(playerSum)
print("Dealers hand: ")
print(dealerList)
print()
print("Total: ")
print(dealerSum)
print("You Lose!")
exit("Game Lost!")
elif (dealerSum < playerSum):
print("---------------------------------------------------------------------------------------------------------------------------------------------------------------------")
print("Final results: ")
print("Players hand: ")
print(playerList)
print()
print("Total: ")
print(playerSum)
print("Dealers hand: ")
print(dealerList)
print()
print("Total: ")
print(dealerSum)
print("You Win!")
exit("Game Won!")
else:
print("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
print("Final results: ")
print("Players hand: ")
print(playerList)
print()
print("Total: ")
print(playerSum)
print("Dealers hand: ")
print(dealerList)
print()
print("Total: ")
print(dealerSum)
print ("It's a tie!")
exit("Game Tied!")
name-MacBook-Air:Python name$ /usr/bin/env /usr/local/bin/python3 /Users/name/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 61743 -- /Users/name/Desktop/Code/Python/boop.py
Traceback (most recent call last):
File "/Users/name/Desktop/Code/Python/boop.py", line 15, in <module>
drawnCard = newDeck.pop(0)
AttributeError: 'NoneType' object has no attribute 'pop'
name-MacBook-Air:Python name$
The error is in how you're shuffling the deck.
newDeck = shuffle(deck)
The random.shuffle function shuffles a list in-place and returns None, so newDeck is getting assigned to None here. This is why Python gives you the error:
AttributeError: 'NoneType' object has no attribute 'pop'
later when you try to pop the first card off of newDeck.
You need to call shuffle(deck) (without assigning it to a new variable) and use the deck variable in the rest of your program.
Hi all programming masters! Im working on a python guessing game and want to change the format of my print to file from "name: win 0: loss 0: guess 0", to "Name | Win or loss (not both) | number of guesses". Im not sure how to make it print the win OR loss, do i need another if statement to print to the txt file?
import random
print("Number guessing game")
name = input("Hello, please input your name: ")
win = 0
loss = 0
guesses = 0
diceRoll = random.randint(1, 6)
if diceRoll == 1:
print("You have 1 guess.")
if diceRoll == 2:
print("You have 2 guesses.")
if diceRoll == 3:
print("You have 3 guesses.")
if diceRoll == 4:
print("You have 4 guesses.")
if diceRoll == 5:
print("You have 5 guesses.")
if diceRoll == 6:
print("You have 6 guesses.")
elif diceRoll != 1 and diceRoll != 2 and diceRoll != 3 and diceRoll != 4 and diceRoll != 5 and
diceRoll != 6:
print("Invalid input!")
number = random.randint(1, 3)
chances = 0
print("Guess a number between 1 and 100:")
while chances < diceRoll:
guess = int(input())
if guess == number:
print("Congratulation YOU WON!!!")
win += 1
guesses = guesses + 1
break
elif guess < number:
print("Your guess was too low")
guesses = guesses + 1
else:
print("Your guess was too high")
guesses = guesses + 1
chances += 1
else:
print("YOU LOSE!!! The number is", number)
loss += 1
print(name)
print("win: "+str(win))
print("loss: "+str(loss))
stat = open("Statistics.txt", "a")
stat.write(name + ":" + " win: "+str(win) + " loss: " +str(loss) +" guesses: "+str(guesses)),
stat.close()
You can solve the critical part using the ternary statement, an "if-then-else" expression, like this:
result = "Win" if win == 1 else "Loss"
stat.write((name + ":" + result + " guesses: "+str(guesses))
Beyond this, I strongly recommend that you look up "Python output format", so you can improve your range of expression. You won't have to keep doing string construction for your output statements.
I currently am making a blackjack python minigame, where a player plays until he gets a blackjack or stands and returns the value of his deck. This game does not play against a dealer and instead just uses his hands as a score. My problem is, I cannot figure out a way to make aces go from 11 to 1 without looping and breaking the program. Here is my code:
import random
def play():
output = "Your hand: "
player = []
total=0
count = 0
while len(player) != 2:
card = random.randint(1,52)
if card <= 4:
output += "A "
total += 11
player.append("A")
elif card <= 8:
output+="2 "
total+=2
player.append("2")
elif card <= 12:
output+="3 "
total+=3
player.append("3")
elif card <= 16:
output+="4 "
total+=4
player.append("4")
elif card <= 20:
output+="5 "
total+=5
player.append("5")
elif card <= 24:
output+="6 "
total+=6
player.append("6")
elif card <= 28:
output+="7 "
total+=7
player.append("7")
elif card <= 32:
output+="8 "
total+=8
player.append("8")
elif card <= 36:
output+="9 "
total+=9
player.append("9")
elif card <= 40:
output+="10 "
total+=10
player.append("10")
elif card <= 44:
output+="J "
total+=10
player.append("J")
elif card <= 48:
output+="Q "
total+=10
player.append("Q")
elif card <= 52:
output+= "K "
total+=10
player.append("K")
if len(player) == 2:
print(f"{output} ({total}) ")
while len(player) >= 2:
action_taken = input("Would you like to 'hit' or 'stand': ")
if action_taken == "hit":
card = random.randint(1,52)
if card <= 4:
output += "A "
total += 11
player.append("A")
elif card <= 8:
output+="2 "
total+=2
player.append("2")
elif card <= 12:
output+="3 "
total+=3
player.append("3")
elif card <= 16:
output+="4 "
total+=4
player.append("4")
elif card <= 20:
output+="5 "
total+=5
player.append("5")
elif card <= 24:
output+="6 "
total+=6
player.append("6")
elif card <= 28:
output+="7 "
total+=7
player.append("7")
elif card <= 32:
output+="8 "
total+=8
player.append("8")
elif card <= 36:
output+="9 "
total+=9
player.append("9")
elif card <= 40:
output+="10 "
total+=10
player.append("10")
elif card <= 44:
output+="J "
total+=10
player.append("J")
elif card <= 48:
output+="Q "
total+=10
player.append("Q")
elif card <= 52:
output+= "K "
total+=10
player.append("K")
if len(player) >= 2 and total <=21:
print(f"{output} ({total}) ")
if total > 21:
if "A" in player: #Ask why ace always messes up
if count < 1:
count +=1
total-=10
print(f"{output} ({total}) ")
if player.count("A") > 1:
total -= 10
print(f"{output} ({total}) ")
else:
print(f"{output} ({total}) ")
print("BUST!")
return total
if action_taken == "stand":
return total
if action_taken != "hit" or "stand":
print("Enter a valid input ('hit' or 'stand') ")
play()
if "A" in player will always be True once there's an ace in the deck, and so you never get to the else where you print "BUST!" and return, so the loop just continues. You can do something like incremeting count for every ace in the deck and then change the ace part to be:
if total > 21:
player_aces = player.count("A") # How many aces the player has
if player_aces != count: # Meaning there are aces that weren't checked
for _ in range(player_aces - count):
total -= 10 # Could also be simplified to: total -= 10 * (player_aces - count)
count = player_aces
print(f"{output} ({total}) ")
else:
print(f"{output} ({total}) ")
print("BUST!")
return total
Also, if action_taken != "hit" or "stand" doesn't check that action_taken is not "hit" and not "stand". An or treats both its inputs as bool values and returns whether at least one is True. The != operator has precedence over or, so the line is actually if (action_taken != "hit") or "stand". The left part of that does what it's supposed to do, but then the right part evaluates "stand" as a bool, and in python every non-empty string is evaluated as True. So the right expression is always True, and so is the or - and the program will always enter the if statement.
You probably want: if action_taken != "hit" and action_taken != "stand".
There were a coupe of issues that I have fixed. I have created a counter for the number of Aces, this allows us to count how many times we can reduced the total by - otherwise we just keep removing 10.
Also the indentation of the last else statement needed moving out.
import random
def play():
output = "Your hand: "
player = []
total=0
count = 0
reducedA = 0
while len(player) != 2:
card = 1
#card = random.randint(1,52)
if card <= 4:
output += "A "
total += 11
reducedA+=1
player.append("A")
elif card <= 8:
output+="2 "
total+=2
player.append("2")
elif card <= 12:
output+="3 "
total+=3
player.append("3")
elif card <= 16:
output+="4 "
total+=4
player.append("4")
elif card <= 20:
output+="5 "
total+=5
player.append("5")
elif card <= 24:
output+="6 "
total+=6
player.append("6")
elif card <= 28:
output+="7 "
total+=7
player.append("7")
elif card <= 32:
output+="8 "
total+=8
player.append("8")
elif card <= 36:
output+="9 "
total+=9
player.append("9")
elif card <= 40:
output+="10 "
total+=10
player.append("10")
elif card <= 44:
output+="J "
total+=10
player.append("J")
elif card <= 48:
output+="Q "
total+=10
player.append("Q")
elif card <= 52:
output+= "K "
total+=10
player.append("K")
if len(player) == 2:
print(f"{output} ({total}) ")
while len(player) >= 2:
action_taken = input("Would you like to 'hit' or 'stand': ")
if action_taken == "hit":
card = random.randint(1,52)
if card <= 4:
output += "A "
total += 11
player.append("A")
reducedA += 1
elif card <= 8:
output+="2 "
total+=2
player.append("2")
elif card <= 12:
output+="3 "
total+=3
player.append("3")
elif card <= 16:
output+="4 "
total+=4
player.append("4")
elif card <= 20:
output+="5 "
total+=5
player.append("5")
elif card <= 24:
output+="6 "
total+=6
player.append("6")
elif card <= 28:
output+="7 "
total+=7
player.append("7")
elif card <= 32:
output+="8 "
total+=8
player.append("8")
elif card <= 36:
output+="9 "
total+=9
player.append("9")
elif card <= 40:
output+="10 "
total+=10
player.append("10")
elif card <= 44:
output+="J "
total+=10
player.append("J")
elif card <= 48:
output+="Q "
total+=10
player.append("Q")
elif card <= 52:
output+= "K "
total+=10
player.append("K")
if len(player) >= 2 and total <=21:
print(f"{output} ({total}) ")
if total > 21:
if "A" in player: #Ask why ace always messes up
if count < 1:
count +=1
total-=10
print(f"{output} ({total}) ")
if player.count("A") > 1 and reducedA:
total -= 10
reducedA -= 1
print(f"{output} ({total}) ")
else:
print(f"{output} ({total}) ")
print("BUST!")
return total
else:
print(f"{output} ({total}) ")
print("BUST!")
return total
if action_taken == "stand":
return total
if action_taken != "hit" or action_taken != "stand":
print("Enter a valid input ('hit' or 'stand') ")
play()
I am trying to code player and mob attacks for a text based RPG game I am making, I have randomint set up for player and mob hitchance and crit chance but I can't figure out how to get a new integer for them every time I restart the loop, it uses the same integer it got the first time it entered the loop.
### GAME VALUES ###
class roll_dice:
def __init__(self):
self.spawn = random.randint(1,100)
self.escape = random.randint(1,100)
self.playercrit = random.randint(1,100)
self.playerhitchance = random.randint(1,100)
self.mobcrit = random.randint(1,100)
self.mobhitchance = random.randint(1,100)
roll = roll_dice()
### ORC SPAWN ###
if fight_walk.lower() == 'fight':
orcMobSpawn()
while True:
fight_orc = input(">>> ")
if fight_orc.lower() == 'a':
### PLAYER ATTACK ###
while True:
roll.playercrit
roll.playerhitchance
if roll.playercrit <= 10 and roll.playerhitchance >= 6:
print("You crit orc for",str(userPlayer.atk * 2),"damage!")
orcMob.hp = orcMob.hp - userPlayer.atk * 2
print("Orc HP:",orcMob.hp)
break
elif roll.playercrit >= 11 and roll.playerhitchance >= 6:
print("You hit orc for",str(userPlayer.atk),"damage!")
orcMob.hp = orcMob.hp - userPlayer.atk
print("Orc HP:",orcMob.hp)
break
elif roll.playercrit >= 11 and roll.playerhitchance <= 5:
print("You missed!")
break
elif roll.playercrit <= 10 and roll.playerhitchance <= 5:
print("You missed!")
break
elif orcMob.hp <= 0 and userPlayer.hp >= 1:
print("Your HP:",str(userPlayer.hp))
print("You win!")
break
elif userPlayer.hp <= 0:
print("You died!")
exit()
### ORC ATTACK ###
while True:
roll.mobcrit
roll.mobhitchance
if orcMob.hp <= 0 and userPlayer.hp >= 1:
break
if roll.mobcrit <= 5 and roll.mobhitchance >= 25:
print("\nOrc crit for",str(orcMob.atk * 2),"damage!")
userPlayer.hp = userPlayer.hp - orcMob.atk * 2
print("Your HP:",str(userPlayer.hp))
break
elif roll.mobcrit >= 5 and roll.mobhitchance >= 25:
print("\nOrc hit for",str(orcMob.atk),"damage!")
userPlayer.hp = userPlayer.hp - orcMob.atk
print("Your HP",str(userPlayer.hp))
break
elif roll.mobcrit <= 5 and roll.mobhitchance <= 25:
print("Orc missed!")
print("Your HP:",str(userPlayer.hp))
break
elif roll.mobcrit >= 5 and roll.mobhitchance <= 25:
print("Orc missed!")
print("Your HP:",str(userPlayer.hp))
break
if orcMob.hp <= 0 and userPlayer.hp >= 1:
break
elif orcMob.hp >= 1:
continue
The problem is with your roll_dice class. You have the values defined when the class initializes, but then you never update them again. Therefore self.escape or self.spawn will always be the same value after the program starts. The easiest way to solve your problem without rewriting much would be to make another instance of roll_dice() every time you want to roll the dice. Something like:
### GAME VALUES ###
class roll_dice:
def __init__(self):
self.spawn = random.randint(1,100)
self.escape = random.randint(1,100)
self.playercrit = random.randint(1,100)
self.playerhitchance = random.randint(1,100)
self.mobcrit = random.randint(1,100)
self.mobhitchance = random.randint(1,100)
# roll = roll_dice() # you don't need to make an instance here
### ORC SPAWN ###
if fight_walk.lower() == 'fight':
orcMobSpawn()
while True:
fight_orc = input(">>> ")
if fight_orc.lower() == 'a':
### PLAYER ATTACK ###
while True:
roll = roll_dice() # make a new instance with each loop
roll.playercrit
roll.playerhitchance
if roll.playercrit <= 10 and roll.playerhitchance >= 6:
print("You crit orc for",str(userPlayer.atk * 2),"damage!")
orcMob.hp = orcMob.hp - userPlayer.atk * 2
print("Orc HP:",orcMob.hp)
break
elif roll.playercrit >= 11 and roll.playerhitchance >= 6:
print("You hit orc for",str(userPlayer.atk),"damage!")
orcMob.hp = orcMob.hp - userPlayer.atk
print("Orc HP:",orcMob.hp)
break
elif roll.playercrit >= 11 and roll.playerhitchance <= 5:
print("You missed!")
break
elif roll.playercrit <= 10 and roll.playerhitchance <= 5:
print("You missed!")
break
elif orcMob.hp <= 0 and userPlayer.hp >= 1:
print("Your HP:",str(userPlayer.hp))
print("You win!")
break
elif userPlayer.hp <= 0:
print("You died!")
exit()
### ORC ATTACK ###
Use functions like
import random
for x in range(10):
print random.randint(1,101)
use a array around for a max 100 people and generate random digits to add in your code.
You can also use a array to create random umber structure and then use the numbers to be shuffled and added as you create them
from random import *
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
shuffle(items)
print items
I'm programming a type of Blackjack game. In this game I want to make it so the dealer and the player each have 100 health and if they lose a hand then they lose 10 health. This goes on until one of them reaches 0 health. I can't figure out how to add health to the game.
Below is the game as of right now:
import random
newgame = 0
def get_card():
#I did random from 1,11 to reduce card counting.
return random.randint(1, 11)
def player():
blackjack = False
total = 0
print('************ YOUR TURN ************')
card1 = get_card()
card2 = get_card()
total = card1 + card2
print("Cards: " + str(card1) + " " + str(card2))
print("Total: " + str(total))
if total is 21:
blackjack = True
while total < 21:
option = raw_input('Type "S" to stay or "H" to hit ')
if option == 's' or option == 'S':
break
card1 = get_card()
print("\nDraws: " + str(card1))
total += card1
print("Total: " + str(total))
return total, blackjack
def dealer():
print("\n********** DEALER'S TURN **********")
total = 0
card1 = get_card()
card2 = get_card()
total = card1 + card2
print("Cards: " + str(card1) + " " + str(card2))
print("Total: " + str(total))
while total <= 16:
raw_input('Press <enter> to continue ...')
card1 = get_card()
print("\nDraws: " + str(card1))
total += card1
print("Total: " + str(total))
return total
def main():
play_again = True
while play_again:
player_total, blackjack = player()
player_wins = False
dealer_wins = False
if blackjack:
print('Blackjack!')
player_wins = True
if player_total > 21:
print('Bust!')
dealer_wins = True
if player_wins is False and dealer_wins is False:
dealer_total = dealer()
if dealer_total > 21:
print('Bust!')
player_wins = True
elif player_total > dealer_total:
player_wins = True
else:
dealer_wins = True
print("\n********** GAME OVER **********")
if player_wins:
print('You win!')
elif dealer_wins:
print('Dealer wins!')
while True:
again = raw_input('Type "P" to play again or "Q" to quit: ')
if again.upper() == "Q":
print("Game ended.")
play_again = False
break
elif again.upper() == "P":
break
main()
You would definitely want to use an object oriented approach if you want to include several players. Refer to this page if you are unfamiliar with it: https://www.tutorialspoint.com/python/python_classes_objects.htm
If you want to just add it to the main function, I would suggest adding a hp = 100 variable. Everytime it's a bust, just deduct 10 from it. If hp == 0, end the game.
just set health to 100 for player and dealer and just take off 10 each time they lose. just decrement player health under dealer wins and dealer health under player wins. fairly simple really