Hello, I'm new nere and very much a beginner with python. I'm trying to make a blackgame program not to play but to test strategies, like I used to play this wristwatch game and always double at 12 and surrender at 17. Which is bad strategy but I swore it worked for that watch game. It seems I am stuck in a loop. (starts and doesn't stop)

from random import choice as rc
def total(hand):
 aces = hand.count(11)
 t = sum(hand)
 if t > 21 and aces > 0:
     while aces > 0 and t > 21:
         t -= 10
         aces -= 1
         return t
cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]

cwin = 0 # computer win counter
pwin = 0 # player win counter  
while True:
     player = []
     player.append(rc(cards))
     player.append(rc(cards))
pbust = False # player busted flag  
cbust = False # computer busted flag
while True:    
      # loop for the player
      tp = total(player)

      if tp > 21:

          pbust = True 
      else:

          if tp < 16:
             player.append(rc(cards))    
          else:
                break
while True:
    comp = []
    comp.append(rc(cards))
    comp.append(rc(cards))
while True:
      tc = total(comp)
      if tc < 17:
          comp.append(rc(cards))    
      else:
          break

      if tc > 21:

        cbust = True
      if pbust == False:
        pwin += 1
      elif tc > tp:
        cwin += 1
      elif tc == tp:
         pwin += 0       
      elif tp > tc:
        if pbust == False:

           pwin += 1

        elif cbust == False:

          cwin += 1

print "pwin" 

I want it to play one hand (for now). Then I could put in the conditions for double which would +2 to counter if win and surrender -.5 iterate it. I ididnt really write this but I had to figure out the indentation, so I dont know if it is right. I wish I knew who did write this so I could thank them maybe paypal them some money or something.

One more thing, I want it to print the value for pwin not the letters "pwin" which I would think is going to happen. Do you know I I would do this? Do you see where I missed a break and the loop is infinite? Thanks for looking.

Daniel

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Aww you were so close, your code tags are almost perfect. All you have to do is have the [/code] tag on its own line. Just repost the code like that and it will have the right indentation and make it easier to us to help you.

As for printing the value of a variable, you can do that by just going

print pwin

Without the "" signs it looks to try and print a variable if you have one :)

Hope that helps

from random import choice as rc
def total(hand):
 aces = hand.count(11)
 t = sum(hand)
 if t > 21 and aces > 0:
     while aces > 0 and t > 21:
         t -= 10
         aces -= 1
         return t
cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
 
cwin = 0 # computer win counter
pwin = 0 # player win counter  
while True:
     player = []
     player.append(rc(cards))
     player.append(rc(cards))
pbust = False # player busted flag  
cbust = False # computer busted flag
while True:    
      # loop for the player
      tp = total(player)
      
      if tp > 21:
          
          pbust = True 
      else:
              
          if tp < 16:
             player.append(rc(cards))    
          else:
                break
while True:
    comp = []
    comp.append(rc(cards))
    comp.append(rc(cards))
while True:
      tc = total(comp)
      if tc < 17:
          comp.append(rc(cards))    
      else:
          break
        
      if tc > 21:
    
        cbust = True
      if pbust == False:
        pwin += 1
      elif tc > tp:
        cwin += 1
      elif tc == tp:
         pwin += 0       
      elif tp > tc:
        if pbust == False:
            
           pwin += 1
                         
        elif cbust == False:
                 
          cwin += 1
       
print pwin

Thanks, I made the change you suggested.

I put in breaks to the 2nd and 4th loops. Now it runs and stops put it does not print anything

from random import choice as rc
def total(hand):
 aces = hand.count(11)
 t = sum(hand)
 if t > 21 and aces > 0:
     while aces > 0 and t > 21:
         t -= 10
         aces -= 1
         return t
cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
 
cwin = 0 # computer win counter
pwin = 0 # player win counter  
while True:
     player = []
     player.append(rc(cards))
     player.append(rc(cards))
pbust = False # player busted flag  
cbust = False # computer busted flag
while True:    
      # loop for the player
      tp = total(player)
      
      if tp > 21:
          
          pbust = True
          break
      else:
              
      if tp < 16:
             player.append(rc(cards))    
      else:
          break
while True:
    comp = []
    comp.append(rc(cards))
    comp.append(rc(cards))
while True:
      tc = total(comp)
      if tc < 17:
          comp.append(rc(cards))    
      else:
          break
        
      if tc > 21:
    
        cbust = True
      if pbust == False:
        pwin += 1
      elif tc > tp:
        cwin += 1
      elif tc == tp:
         pwin += 0       
      elif tp > tc:
      if pbust == False:
            
           pwin += 1
                    
      elif cbust == False:
                 
          cwin += 1
          break
       
print pwin

Add some print statements to see what is going on. I have added two in the following code.

from random import choice as rc
def total(hand):
 aces = hand.count(11)
 t = sum(hand)
 if t > 21 and aces > 0:
     while aces > 0 and t > 21:
         t -= 10
         aces -= 1
         return t
cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
 
cwin = 0 # computer win counter
pwin = 0 # player win counter  
while True:
     player = []
     player.append(rc(cards))
     player.append(rc(cards))
     print "player list now =", player
    
pbust = False # player busted flag
cbust = False # computer busted flag
while True:    
      # loop for the player
      tp = total(player)
      print "total", tp, player
    
      if tp > 21:
          
          pbust = True
          break
      elif tp < 16:
             player.append(rc(cards))    
      else:
          break
while True:
    comp = []
    comp.append(rc(cards))
    comp.append(rc(cards))
while True:
      tc = total(comp)
      if tc < 17:
          comp.append(rc(cards))    
      else:
          break
        
      if tc > 21:
    
        cbust = True
      if pbust == False:
        pwin += 1
      elif tc > tp:
        cwin += 1
      elif tc == tp:
         pwin += 0       
      elif tp > tc:
         if pbust == False:
            
           pwin += 1
                    
      elif cbust == False:
                 
          cwin += 1
          break
       
print pwin
This article has been dead for over six months. Start a new discussion instead.