So I got bored and made a blackjack game. This is probably be the last thing I code in python before I move to C#!!!(so excited to learn it)

The game() function is the main fuction all the rest are pretty self explanatory if you understand blackjack. My issue is that when you deal the cards and it ask if you want to hit it gives me an error no matter the input.

#Black Jack

import random


# Main function
def game():
	computerHand = deal()
	playerHand = deal()
	print "Welcome to Blackjack!"
	print "---START GAME---"
		#begin/deal the card 
	start=raw_input("Deal cards? [Y/N]")
	global playerHand
	global computerHand
	if start=="Y" or "y":
		print "Your cards are: "+str(playerHand)
		print "Dealer's Cards are:"+str(computerHand)
		hit=raw_input("Do you want to hit? [Y/N] ")
		if hit=="Y" or "y":
			hit()
			#check it hit busted player
			bust()
			print str(playerHand)
		else:
			print "You stand with"+str(playerHand)
	else:
		print "Fine then, Don't play..."
			
		
		
		
			
	



#Deal player/comp new hand
def deal():
	random1 = random.randint(1,14)
	random2 = random.randint(1,14)

	#first card/Converts face card to accual value
	if random1 == 11:
		random1 = "J"
	if random1 == 12:
		random1 = "Q"
	if random1 == 13:
		random1 = "K"
	if random1 == 14:
		random1 = "A"
	
	#second card
	if random2 == 11:
		random2 = "J"
	if random2 == 12:
		random2 = "Q"
	if random2 == 13:
		random2 = "K"
	if random2 == 14:
		random2 = "A"
	hand = [random1,random2]
	return hand




def hit():
	newCard = random.randint(1,14)
	if newCard == 11:
		newCard = "J"
	
	if newCard == 12:
		newCard = "Q"
	
	if newCard == 13:
		newCard = "K"
	
	if newCard == 14:
		newCard = "A"	
	
	global playerHand
	playerHand.append(str(newCard))


def bust():
	"""Detect if player hitting put him over 21"""
	if playerHand>21:
		print "Your over 21 your BUSTED!"
	elif playerHand==21:
		print "You got blackjack! :]"
	elif playerHand<21:
		#PLAYER is good and not busted
		pass
		
def dealer():
	"""Defines dealers stragety"""
	global computerHand
	if computerHand>=15:
		pass
	elif computerHand<15:
		hit()

#This is the console input that results in error.

Welcome to Blackjack!

---START GAME---

Deal cards? [Y/N]y

Your cards are: [4, 7]

Dealer's Cards are:['K', 3]

Do you want to hit? [Y/N] y

Traceback (most recent call last):
  File "/private/var/folders/3W/3Wn7b1HVGVG3oJSMntLupk+++TI/Cleanup At Startup/BlackJack-295612489.335.py", line 113, in <module>
    game()
  File "/private/var/folders/3W/3Wn7b1HVGVG3oJSMntLupk+++TI/Cleanup At Startup/BlackJack-295612489.335.py", line 21, in game
    hit()
TypeError: 'str' object is not callable
logout
TrustyTony commented: Unclear logic, incorrect implementation +0

Recommended Answers

All 7 Replies

Global varible 'hit' which is user input is string and can not use as function.
I do not understand your program. I can not find the deck of cards, dealing. Picture cards seem not to have value 10. I also do not see handling of ace as 1 or 10 depending of hand value.

Yeah, Theres alot of things I havent got to yet.

But since hit the input variable and the function hit and named the same they cause an error?

Also
if hit=="Y" or "y":
is just plain old wrong!

Good luck with the exciting C# language.

function is value same as 1 or 'a'

Problem would not have occured if the input part were in separate function.

If you are not too good at Python, give Ruby a try!

If you still decide to carry through to do your project, maybe you should consider to think through the logic of the game and restart from these few lines adapted from my own blackjack program (Tkinter Button use practise):

from __future__ import print_function

import random
def init_vars():
    global hand,deck

    deck=[(x+1,y) for x in range(13) for y in ['hearts','diamond','club','spade']]
    random.shuffle(deck)

    hand=dict(deck)

    winner=('',0)
    hand['dealer']=[deck.pop(),deck.pop()] ## initial dealt

init_vars()

print('Dealer hand is:')
for value,kind in hand['dealer']:
    print("%i of %s" % (value,kind))

print('\nThe cards left in deck are:')
for i,(value,kind) in enumerate(deck):
    print("%2i: %2i of %8s," % (i,value,kind),end='\t')

I think im just gonna give this one up. I rushed into it and its all wrong...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.