import random import math def all_indices(value, qlist): indices = [] idx = -1 while True: try: idx = qlist.index(value, idx+1) indices.append(idx) except ValueError: break return indices def score(n): s1=0 s2=0 t=0 num_aces=len(all_indices(1,n)) for z in n: if z==1: pass elif z>10: s1=s1+10 else: s1=s1+z if s1<21: if num_aces>0: st=(s1+11*num_aces) if st<=21: s2=11*num_aces elif st>21: while (st>21) and (t<=num_aces): s2=11*(num_aces-t)+t st=s1+s2 t=t+1 elif num_aces==0: s=s1 else: print('help me') s=s1+s2 if s>21: s=0 return s def deal(n): cards=[] for x in range(0,n): z=math.ceil(random.random()*13) cards.append(z) return(cards) def dealer(cards): z=score(cards) while (z<17) and (z!=0) : cards.append(sum(deal(1))) z=score(cards) if score(cards)>21: z=0 return z,cards def criteria(dealer_first): dealt=0 average=(2+3+4+5+6+7+8+9+10+10+10+10+(11+1)/2)/13 guess=dealer_first+average while guess<17: guess=guess+average dealt=dealt+1 if guess>21: c=dealer_first+average elif guess<=21: c=guess else: print('what the heck man') return c def player(cards,dealer_first): z=score(cards) stay=0 bust=0 while stay==0: if z>21: bust=1 stay=1 if z>criteria(dealer_first): stay=1 else: cards.append(sum(deal(1))) z=score(cards) return z,cards for n in range(0,3): pc=deal(2) dc=deal(2) fp=player(pc,dc[0]) fd=dealer(dc) if fp<fd: print('loss') if fp>fd: print('win') if fp==fd: print('push')
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question