Python and global declaration - python

I'm having trouble trying to resolve this error:
SyntaxWarning: name 'meaning5' is assigned to before global declaration
Basically my program needs to allow the user to input their name, the program then calculates the users lucky number based on the assignment of a=1, b=2 etc.
This is my code so far:
from time import sleep
tempNumb = 0
tempLNN1a = 0
tempLNN1b = 0
tempLNN2a = 0
tempLNN2b = 0
varLNN1 = 0
varLNN2 = 0
LNN = 0
tempLNNa = 0
tempLNNb = 0
templetter = "nothing"
meaning1 = "Natural leader"
meaning2 = "Natural peacemaker"
meaning3 = "Creative and optimistic"
meaning4 = "Hard worker"
meaning5 = "Value freedom"
meaning6 = "Carer and a provider"
meaning7 = "Thinker"
meaning8 = "Have diplomatic skills"
meaning9 = "Selfless and generous"
global templetter
global tempNumb
global tempLNN1a
global tempLNN1b
global tempLNN2a
global tempLNN2b
global varLNN1
global varLNN1
global LNN
global tempLNNa
global tempLNNb
global meaning1
global meaning2
global meaning3
global meaning4
global meaning5
global meaning6
global meaning7
global meaning8
global meaning9
def mainprogram():
sleep(1)
print("-----------------------")
print("Welcome to LUCKY NAME \n NUMBERS")
print("-----------------------")
sleep(1)
firstname = input("Please input your first \nname in all capitals\n")
if firstname == firstname.upper():
print("-----------------------")
sleep(1)
surname = input("Please input your surname \nin all capitals\n")
if surname == surname.upper():
print("-----------------------")
print("Calculating your Lucky \nName Number...")
for i in range(len(firstname)):
templetter = firstname[i]
calculate()
tempfirstname()
for i in range(len(surname)):
templetter = surname[i]
calculate()
tempsurname()
finalcalculate()
def calculate():
if templetter == "A":
tempNumb = 1
elif templetter == "B":
tempNumb = 2
elif templetter == "C":
tempNumb = 3
elif templetter == "D":
tempNumb = 4
elif templetter == "E":
tempNumb = 5
elif templetter == "F":
tempNumb = 6
elif templetter == "G":
tempNumb = 7
elif templetter == "H":
tempNumb = 8
elif templetter == "I":
tempNumb = 9
elif templetter == "J":
tempNumb = 1
elif templetter == "K":
tempNumb = 2
elif templetter == "L":
tempNumb = 3
elif templetter == "M":
tempNumb = 4
elif templetter == "N":
tempNumb = 5
elif templetter == "O":
tempNumb = 6
elif templetter == "P":
tempNumb = 7
elif templetter == "Q":
tempNumb = 8
elif templetter == "R":
tempNumb = 9
elif templetter == "S":
tempNumb = 1
elif templetter == "T":
tempNumb = 2
elif templetter == "U":
tempNumb = 3
elif templetter == "V":
tempNumb = 4
elif templetter == "W":
tempNumb = 5
elif templetter == "X":
tempNumb = 6
elif templetter == "Y":
tempNumb = 7
elif templetter == "Z":
tempNumb = 8
else:
"You managed to break it."
mainprogram()
def tempfirstname():
varLNN1 = varLNN1 + tempNumb
def tempsurname():
varLNN2 = varLNN2 + tempNumb
def finalcalculate():
varLNN1 = str(varLNN1)
varLNN2 = str(varLNN2)
tempLNN1a = varLNN1[0]
tempLNN1b = varLNN1[1]
tempLNN2a = varLNN2[0]
tempLNN2b = varLNN2[1]
varLNN1 = int(tempLNN1a) + int(tempLNN1b)
varLNN2 = int(tempLNN2a) + int(tempLNN2b)
LNN = varLNN1 + varLNN2
LNN = str(LNN)
tempLNNa = LNN[0]
tempLNNb = LNN[1]
LNN = int(tempLNNa) + int(tempLNNb)
if LNN == 1 or "1":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning1)
loop()
elif LNN == 2 or "2":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning2)
loop()
elif LNN == 3 or "3":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are " + meaning3)
loop()
elif LNN == 4 or "4":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning4)
loop()
elif LNN == 5 or "5":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you " + meaning5)
loop()
elif LNN == 6 or "6":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning6)
loop()
elif LNN == 7 or "7":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are a " + meaning7)
loop()
elif LNN == 8 or "8":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you " + meaning8)
loop()
elif LNN == 9 or "9":
print("Your Lucky Name Number is - " + str(LNN) + " and it means you are " + meaning9)
loop()
else:
print("Somehow your lucky name number is too high...")
mainprogram()

Python is different than C, you don't have to declare a variable global, if you are using a global variable in a function, there you need to use global keyword.
For example:
meaning5 = "Value freedom"
def somefunction():
global meaning5
print meaning5
and as linsug has said use lists.

Simply define all the global variable in a single function like :
def global_variables():
global var_1
global var_2
global var_3
global var_4
.
global var_n
And before calling anything else in main, call this function first:
if __name__ == "__main__":
global_variables()
# Rest of your code.
Might it help!. Thanx

Related

Why Soccer game slips right past halftime and 1st half stoppage time Python

I'm trying to figure a way to get 3 minutes of stoppage time without copying and pasting the whole code into an extra 3 minute sequence. The rest of the code works well except when I inserted the fsthalf = fsthalf - 3. That's when the code skipped over halftime and full time. Otherwise the code works decently. Any suggestions?
import random
import time
pscore = 0
cscore = 0
fsthalf = 0
extrafst = 0
extrasec = 0
Player1 = input("Enter 1 or 2 =")
Player2 = input("Enter 1 or 2 =")
Player3 = input("Enter 1, 2, or 3 =")
Player4 = input("Enter 1, 2, 3, or 4 =")
cpu1 = ["1", "2"]
cpu2 = ["1", "2"]
cpu3 = ["1", "2", "3"]
cpu4 = ["1", "2", "3", "4"]
cpu4 = ("1", "2", "3", "4")
run = ["1", "2"]
pass1 = ["1", "2"]
pass2 = ["1", "2", "3"]
shot = ["1", "2", "3", "4"]
half = True
while half:
if fsthalf == 45 + 3:
print("Halftime")
time.sleep(2)
fsthalf == fsthalf - 3
if Player1 == random.choice(run):
print("Player has the ball")
time.sleep(1)
print("Player makes a run")
fsthalf = fsthalf + 1
print("time", fsthalf)
if Player2 == random.choice(pass1):
print("Player Pass connects")
if Player3 == random.choice(pass2):
print("Great pass in the box")
if Player4 == random.choice(shot):
print("Goooooal!!!")
pscore = pscore + 1
print("Player", pscore)
print("CPU", cscore)
elif Player4 != random.choice(shot):
print("What A Save By CPU!")
pass
elif Player3 != random.choice(pass2):
print("Player Pass stolen")
pass
elif Player2 != random.choice(pass1):
print("Player turnover on pass")
pass
elif Player1 != random.choice(run):
print("Player Turnover at Midfield!")
time.sleep(1)
pass
if random.choice(cpu1) == random.choice(run):
print("CPU has the ball")
time.sleep(1)
print("CPU makes a run")
fsthalf = fsthalf + 1
print("time", fsthalf)
if random.choice(cpu2) == random.choice(pass1):
print("CPU Pass connects")
if random.choice(cpu3) == random.choice(pass2):
print("Great pass in the box")
if random.choice(cpu4) == random.choice(shot):
print("Goooooal!!!")
cscore = cscore + 1
print("Player", pscore)
print("CPU", cscore)
elif random.choice(cpu4) != random.choice(shot):
print("What A Save By Player!")
pass
elif random.choice(cpu3) != random.choice(pass2):
print("CPU Pass stolen")
pass
elif random.choice(cpu2) != random.choice(pass1):
print("CPU turnover on pass")
pass
elif random.choice(cpu1) != random.choice(run):
print("CPU Turnover at Midfield!")
time.sleep(1)
pass
if fsthalf == 90 + 3:
print("Full Time!")
print("Player", pscore)
print("CPU", cscore)
half = False
I'm sure it has something to do with the fact that your code says fsthalf == fsthalf - 3 instead of fsthalf = fsthalf - 3

Program does not loop

I'm curious as to why this program is not looping. The program will run once but when another game is played it doesn't record the winner and quits.
Any assistance will be greatly appreciated!
Here is the Python program:
import random
random.seed()
print ("For each round please select from the following warriors: ")
print ("'C' or 'c' for Cowboy")
print ("'N' or 'n' for Ninja")
print ("'B' or 'b' for Bear")
print ("'Q' or 'q' for quit")
print()
gamesPlayed = 0
print ("Round", gamesPlayed +1, ":")
warriorStr = input("Please choose a warrior: ")
warriorStr = warriorStr.lower()
while not warriorStr.isalpha():
print()
print ("That's not a valid choice!")
warriorStr = input("Please enter a weapon: ")
computer = random.choice("cnb")
count = 0
winCount = 0
lossCount = 0
tieCount = 0
if warriorStr == "n" or "N" and computer == "c" or "C":
winCount = winCount + 1
print ("You win")
elif warriorStr == "c" or "C" and computer == "b" or "B":
winCount = winCount + 1
print ("You win")
elif warriorStr == "b" or "B" and computer == "n" or "N":
winCount = winCount + 1
print ("You win")
elif warriorStr == "c" and computer == "n":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "b" and computer == "c":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "n" and computer == "b":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "c" and computer == "c":
tieCount = tieCount + 1
print ("You tied")
elif warriorStr == "n" and computer == "n":
tieCount = tieCount + 1
print ("You tied")
elif warriorStr == "b" and computer == "b":
tieCount = tieCount + 1
print ("You tied")
gamesPlayed = gamesPlayed + 1
print()
print ("Round", gamesPlayed +1, ":")
warriorStr = input("Choose a warrior: ")
if warriorStr == "q" and "Q":
print("Game Over!")
print ("You have played a total of", gamesPlayed, "games.")
print ("You won", winCount, "times")
print ("The computer won", lossCount, "times")
print ("You tied", tieCount, "times")
Wrap the whole thing in a while loop.
Try something like this:
warriorStr = input("Please choose a warrior: ")
warriorStr = warriorStr.lower()
acceptables = ['a','c','n','q']
while warriorStr not 'q' and warriorStr in acceptables:
warriorStr = input("Please enter a weapon: ")
computer = random.choice("cnb")
count = 0
winCount = 0
lossCount = 0
tieCount = 0
if warriorStr == "n" or "N" and computer == "c" or "C":
winCount = winCount + 1
print ("You win")
elif warriorStr == "c" or "C" and computer == "b" or "B":
winCount = winCount + 1
print ("You win")
elif warriorStr == "b" or "B" and computer == "n" or "N":
winCount = winCount + 1
print ("You win")
elif warriorStr == "c" and computer == "n":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "b" and computer == "c":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "n" and computer == "b":
lossCount = lossCount + 1
print ("Computer wins")
elif warriorStr == "c" and computer == "c":
tieCount = tieCount + 1
print ("You tied")
elif warriorStr == "n" and computer == "n":
tieCount = tieCount + 1
print ("You tied")
elif warriorStr == "b" and computer == "b":
tieCount = tieCount + 1
print ("You tied")
gamesPlayed = gamesPlayed + 1
print()
print ("Round", gamesPlayed +1, ":")
warriorStr = input("Choose a warrior: ")
print("Game Over!")
print ("You have played a total of", gamesPlayed, "games.")
print ("You won", winCount, "times")
print ("The computer won", lossCount, "times")
print ("You tied", tieCount, "times")

Naughts and crosses in python: scoring not working

I realise that many other people have tried and failed to make naughts and crosses (by looking at the suggested question) but I'm assuming they don't have the same problem as me.
Here is my code:
import random, time, sys
raw3 = ["-", "-", "-"]
raw2 = ["-", "-", "-"]
raw1 = ["-", "-", "-"]
def draw():
str3 = ""
str2 = ""
str1 = ""
for i in raw3:
str3 = (str3 + i + " ")
for i in raw2:
str2 = (str2 + i + " ")
for i in raw1:
str1 = (str1 + i + " ")
str_board = str3+str("\n"*2)+str(str2)+str("\n"*2)+str(str1)
print(str_board)
def playerX_turn():
c_choice_in = input("Type the coordinates in the format y/x\n(e.g. 2/1)")
c_choice_list = list(c_choice_in)
c_choice_x = int(c_choice_list[0]); c_choice_y = int(c_choice_list[2])
print(c_choice_x, c_choice_y)
if c_choice_x == 3 and raw3[c_choice_y - 1] == "-":
raw3[c_choice_y - 1] = "X"
elif c_choice_x == 2 and raw2[c_choice_y - 1] == "-":
raw2[c_choice_y - 1] = "X"
elif c_choice_x == 1 and raw1[c_choice_y - 1] == "-":
raw1[c_choice_y - 1] = "X"
def playerO_turn():
c_choice_in = input("Type the coordinates in the format y/x\n(e.g. 2/1)")
c_choice_list = list(c_choice_in)
c_choice_x = int(c_choice_list[0]); c_choice_y = int(c_choice_list[2])
print(c_choice_x, c_choice_y)
if c_choice_x == 3 and raw3[c_choice_y - 1] == "-":
raw3[c_choice_y - 1] = "O"
elif c_choice_x == 2 and raw2[c_choice_y - 1] == "-":
raw2[c_choice_y - 1] = "O"
elif c_choice_x == 1 and raw1[c_choice_y - 1] == "-":
raw1[c_choice_y - 1] = "O"
def check4win():
winner = None
if winner == None or winner == False and raw3[0] == "X" or raw3[2] == "X" and raw2[1] == "X" and raw1[0] == "X" or raw1[2] == "X":
winner = "playerX"
column_checker = 0
for i in range(0,3):
if winner == None or winner == False and raw3[column_checker] == "X" and raw2[column_checker] == "X" and raw1[column_checker] == "X":
winner = "playerX"
else:
column_checker += 1
if winner == None or winner == False:
if raw3[0] == "X" and raw3[1] == "X" and raw3[2] == "X":
winner = "playerX"
elif raw2[0] == "X" and raw2[1] == "X" and raw2[2] == "X":
winner = "playerX"
elif raw1[0] == "X" and raw1[1] == "X" and raw1[2] == "X":
winner = "playerX"
### ### ### ###
if winner == None or winner == False and raw3[0] == "Y" or raw3[2] == "Y" and raw2[1] == "Y" and raw1[0] == "Y" or raw1[2] == "Y":
winner = "playerY"
column_checker = 0
for i in range(0,3):
if winner == None or winner == False and raw3[column_checker] == "Y" and raw2[column_checker] == "Y" and raw1[column_checker] == "Y":
winner = "playerY"
else:
column_checker += 1
if winner == None or winner == False:
if raw3[0] == "Y" and raw3[1] == "Y" and raw3[2] == "Y":
winner = "playerY"
elif raw2[0] == "Y" and raw2[1] == "Y" and raw2[2] == "Y":
winner = "playerY"
elif raw1[0] == "Y" and raw1[1] == "Y" and raw1[2] == "Y":
winner = "playerY"
### ### ### ###
if winner != None or winner != False:
if winner == "playerX":
print("X won the game!\n(Sorry, O!)")
elif winner == "playerY":
print("Y won the game!\n(Bad luck, X!)")
sys.exit()
def main():
game_over = False
while game_over == False:
playerX_turn()
draw()
check4win()
playerO_turn()
draw()
check4win()
main()
So my first problem is that this is an example of output from it:
>>>
Type the coordinates in the format y/x
(e.g. 2/1)1,1
1 1
- - -
- - -
X - -
X won the game!
(Sorry, O!)
>>>
Which clearly is not correct...
It's probably a stupid mistake somewhere but I'm too lazy to go looking through it properly, but I've had a brief look through the check4win() function, but to no avail.
Another (less important) thing is that I have a feeling this code could be made neater... Maybe something with main()?

Python not detecting when a variable is equal to another variable

I am making a program for my family for keeping ping pong scores. When the score limit is reached, it should end the game and say who won. This works for one player but not the other, and I can't figure out why. When P2's score reaches 11, it says that P2 wins and restarts, but when P1's score reaches 11, it just keeps on going. Please keep in mind that I am new to this. Here is the code:
import sys
def defaults():
global p1sc
global p2sc
global p1adv
global p2adv
global p1name
global p2name
global sclimit
p1sc = 0
p2sc = 0
p1adv = 0
p2adv = 0
p1name = "Player 1"
p2name = "Player 2"
sclimit = 11
def checkwin():
global p1sc
global p2sc
global p1name
global p2name
global sclimit
if p2sc < sclimit:
program()
if p1sc < sclimit:
program()
if p2sc <= sclimit:
print(p2name + " wins!")
defaults()
print("New game!")
program()
if p1sc <= sclimit:
print(p1name + " wins!")
defaults()
print("New game!")
program()
elif p1sc == sclimit - 1 and p2sc == sclimit - 1:
print("Sudden Death!")
suddendeath()
else:
print("DEBUG: CHECKWIN FAILED")
def displayscore():
global p1name
global p2name
global p1sc
global p2sc
print(p1name + ": " + str(p1sc))
print(p2name + ": " + str(p2sc))
def program():
global p1sc
global p2sc
global p1name
global p2name
global sclimit
cmdinput = str(raw_input("Command: "))
if cmdinput == "1":
p1sc = p1sc + 1
displayscore()
checkwin()
elif cmdinput == "2":
p2sc = p2sc + 1
displayscore()
checkwin()
elif cmdinput == "q":
p1sc = p1sc - 1
displayscore()
checkwin()
elif cmdinput == "w":
p2sc = p2sc - 1
displayscore()
checkwin()
elif cmdinput == "name":
p1name = str(raw_input("Player 1 name: "))
print("Player 1 name has been set to " + p1name)
p2name = str(raw_input("Player 2 name: "))
print("Player 2 name has been set to " + p2name)
program()
elif cmdinput == "limit":
sclimit = raw_input("Score limit: ")
print("Score limit has been set to " + str(sclimit))
program()
elif cmdinput == "exit":
sys.exit()
else:
print("Command not recognised")
program()
def suddendeath():
global p1name
global p2name
global p1adv
global p2adv
cmdinput = str(raw_input("Command: "))
if cmdinput == "1":
if p2adv == 0:
p1adv = 1
elif p2adv == 1:
p2adv = 0
elif p1adv == 1:
print(p1name + " wins!")
defaults()
print("New game!")
program()
elif cmdinput == "2":
if p1adv == 0:
p2adv = 1
elif p1adv == 1:
p1adv = 0
elif p2adv == 1:
print(p2name + " wins!")
defaults()
print("New game!")
program()
defaults()
program()
Your progam has some problems with the conditions, this should work now:
def checkwin():
...
#call program only if both player's score is less than the limit
if p2sc < sclimit and p1sc < sclimit:
program()
#use if-elif conditions here, and you need `>=` here not `<=`.
if p2sc >= sclimit:
print(p2name + " wins!")
defaults()
print("New game!")
program()
elif p1sc >= sclimit:
print(p1name + " wins!")
defaults()
print("New game!")
program()
...
Demo:(Changed sclimit to 3)
$ python so.py
Command: 1
Player 1: 1
Player 2: 0
Command: 1
Player 1: 2
Player 2: 0
Command: 2
Player 1: 2
Player 2: 1
Command: 1
Player 1: 3
Player 2: 1
Player 1 wins!
New game!
Command: 1
Player 1: 1
Player 2: 0
Command: 2
Player 1: 1
Player 2: 1
Command: 2
Player 1: 1
Player 2: 2
Command: 2
Player 1: 1
Player 2: 3
Player 2 wins!
New game!
Command:

Variables not being detected in blackjack program in Python

I wrote the following program to play blackjack with the user, but whenever a player gets a Jack, Queen, or King, the if statements in the total_value function do not detect them. What should I do to fix this? Also, do you have any general pointers to clean up my code or make my syntax better?
import random
from random import randint
class deck():
"""This class holds the deck information"""
clubs = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
spades = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
hearts = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
diamonds = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
suites = ["clubs", "spades", "hearts", "diamonds"]
def drawcard(self):
#This method removes a card from the deck and returns it.
#Do not draw cards when there are none left.
if self.suites:
suite = random.choice(self.suites)
suite_number = randint(0, len(self.suites) - 1)
if suite == "clubs":
card = (self.clubs).pop(randint(0, len(self.clubs) - 1))
if not self.clubs:
(self.suites).remove("clubs")
elif suite == "spades":
card = (self.spades).pop(randint(0, len(self.spades) - 1))
if not self.spades:
(self.suites).remove("spades")
elif suite == "hearts":
card = (self.hearts).pop(randint(0, len(self.hearts) - 1))
if not self.hearts:
(self.suites).remove("hearts")
elif suite == "diamonds":
card = (self.diamonds).pop(randint(0, len(self.diamonds) - 1))
if not self.diamonds:
(self.suites).remove("diamonds")
return card, suite
else:
return "OUT", "CARDS ERROR"
def total_value(hand):
#This function returns the current total value of a player's hand.
#For example, ["A", "K"] would return 21.
value = 0
aces = 0
for card in hand:
if card == "A":
aces = aces + 1
elif card == "J":
value == value + 10
elif card == "Q":
value == value + 10
elif card == "K":
value == value + 10
else:
value = value + card
if aces == 1:
if value <= 10:
value = value + 11
elif value > 10:
value = value + 1
elif aces == 2:
if value <= 9:
value = value + 12
elif value > 9:
value = value + 2
elif aces == 3:
if value <= 8:
value = value + 13
elif value > 8:
value = value + 3
elif aces == 4:
if value <= 7:
value = value + 14
elif value > 7:
value = value + 4
return value
new_deck = deck()
player_hand = [ ]
card1 = new_deck.drawcard()
card2 = new_deck.drawcard()
print "You have drawn a " + str(card1[0]) + " of " + card1[1] + " and a " + str(card2[0]) + " of " + card2[1] + "!"
player_hand.append(card1[0])
player_hand.append(card2[0])
dealer_hand = [ ]
card3 = new_deck.drawcard()
card4 = new_deck.drawcard()
dealer_hand.append(card3[0])
dealer_hand.append(card4[0])
gameover = False
win = False
dealer_finished = False
player_finished = False
while not gameover:
while dealer_finished == False:
if total_value(dealer_hand) < 17:
card = new_deck.drawcard()
dealer_hand.append(card[0])
elif total_value(dealer_hand) >= 17:
dealer_finished = True
if total_value(dealer_hand) > 21:
print "Dealer Busts!"
win = True
gameover = True
break
while player_finished == False:
choice = raw_input("Hit or Stay? ")
choice.capitalize()
if choice == "Hit":
card = new_deck.drawcard()
player_hand.append(card[0])
print "You drew a", card[0], "of " + card[1]
if total_value(player_hand) > 21:
player_finished = True
break
elif choice == "Stay":
player_finished = True
gameover = True
break
else:
print "Invalid Option"
if total_value(player_hand) > 21:
win == False
gameover == True
break
gameover == True
if win == True:
print "Congratulations!"
else:
print total_value(player_hand), total_value(dealer_hand)
if total_value(player_hand) > 21:
print "You bust!"
elif total_value(dealer_hand) > total_value(player_hand):
print "The dealer had", str(total_value(dealer_hand)), "but you only had", str(total_value(player_hand)) + "."
print "You lose!"
elif total_value(dealer_hand) == total_value(player_hand):
print "You tie the dealer with", total_value(dealer_hand)
elif total_value(dealer_hand) < total_value(player_hand):
print "The dealer had", str(total_value(dealer_hand)), "and you had", str(total_value(player_hand)) + ", so you win!"
You have double-equals signs for the J, Q, and K cases:
if card == "A":
aces = aces + 1
elif card == "J":
value == value + 10 #xxx
elif card == "Q":
value == value + 10 #xxx
elif card == "K":
value == value + 10 #xxx
else:
value = value + card
All this does is check whether value is value + 10 and evaluate to True or False. You want to assign the value:
if card == "A":
aces = aces + 1
elif card == "J":
value = value + 10
elif card == "Q":
value = value + 10
elif card == "K":
value = value + 10
else:
value = value + card
or better yet:
if card == "A":
aces = aces + 1
elif card == "J":
value += 10
elif card == "Q":
value += 10
elif card == "K":
value += 10
else:
value += card

Categories