|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Blackjack code
here i have black jack code, which does not seem to run, as it keeps displaying syntax error. i dont seem to understand the problem and how to solve it, and was hoping if anyone can help me out, below is the code, if any one can copy it in to python, and see y it does not run. thankyou
Code:
from random import choice as randomcards
def total(hand):
# how many aces in the hand
aces = hand.count(11)
# to complicate things a little the ace can be 11 or 1
# this little while loop figures it out for you
t = sum(hand)
# you have gone over 21 but there is an ace
if t > 21 and aces > 0:
while aces > 0 and t > 21:
# this will switch the ace from 11 to 1
t -= 10
aces -= 1
return t
# a suit of cards in blackjack assume the following values
cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
# there are 4 suits per deck and usually several decks
# this way you can assume the cards list to be an unlimited pool
cwin = 0 # computer win counter
pwin = 0 # player win counter
while True:
player = []
# draw 2 cards for the player to start
player.append(rc(cards))
player.append(rc(cards))
pbust = False # player busted flag
cbust = False # computer busted flag
while True:
# loop for the player's play ...
tp = total(player)
print "The player has these cards %s with a total value of %d" % (player, tp)
if tp > 21:
print "--> The player is busted!"
pbust = True
break
else tp == 21:
print "\a BLACKJACK!!!"
break
else:
hs = raw_input("Hit or Stand/Done (h or s): ").lower()
if 'h' in hs:
player.append(rc(cards))
else:
break
while True:
# loop for the computer's play ...
comp = []
comp.append(rc(cards))
comp.append(rc(cards))
# dealer generally stands around 17 or 18
while True:
tc = total(comp)
if tc < 18:
comp.append(rc(cards))
else:
break
print "the computer has %s for a total of %d" % (comp, tc)
# now figure out who won ...
if tc > 21:
print "--> The computer is busted!"
cbust = True
if pbust == False:
print "The player wins!"
pwin += 1
elif tc > tp:
print "The computer wins!"
cwin += 1
elif tc == tp:
print "It's a draw!"
elif tp > tc:
if pbust == False:
print "The player wins!"
pwin += 1
elif cbust == False:
print "The computer wins!"
cwin += 1
break
print
print "Wins, player = %d computer = %d" % (pwin, cwin)
exit = raw_input("Press Enter (q to quit): ").lower()
if 'q' in exit:
break
print
print "Thanks for playing blackjack with the computer!"
|
|
#2
|
|||
|
|||
|
please use code tags when posting code examples as indentation is important in python, or better yet use [ highlight = "python" ] your code here[ / highlight ] without the spaces in the []
|
|
#3
|
|||
|
|||
|
sorry for that, i have now edited, the message, and have put the code, in code tags.
|
|
#4
|
|||
|
|||
|
Your problem is here
Code:
else tp == 21: else doesn't take in conditional in this way because it is designed to be run if all previous conditionals fail (i hope that makes sense). You'll also want to fix the indentation after this as its kinda all over the place Last edited by c-2501 : December 18th, 2008 at 05:55 AM. |
|
#5
|
|||
|
|||
|
ok understand that, how would i fix this, problem, for example how should it be instead, yuo have siad that there is a problem with "else tp == 21:" so how would i fix this.
|
|
#6
|
|||
|
|||
|
I would have thought that was fairly obvious as you have used multiple if statements elsewhere in the code.
|
|
#7
|
|||
|
|||
|
i cant see the problem is. is possiable if u could show how i would correct it.
thankyou |
|
#8
|
|||
|
|||
|
I think perhaps you need to go back over the fundamentals of the language rather than jumping into this blackjack code you've come across, as basic conditional statements are at the very heart of how programs operate.
|
|
#9
|
|||
|
|||
|
can u please show me, how i would sort thsi problem out, with the "else tp == 21:" t
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Blackjack code |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|