プログラミング初心者です。以下のソースですが、どこに問題があるかわかりません。助けてください。
ブラックジャックゲームを作成しています。
実行時にエラーが発生します。スタンドかヒットかダブルかをインプットで数字を入力したあと、後続の処理に進みません。
環境はatomを使用しております。Python 3.6.5
発生している問題・エラーメッセージ
STAND:1, HIT:2, DOUBLE:3 > 1
STAND:1, HIT:2, DOUBLE:3 > 1
STAND:1, HIT:2, DOUBLE:3 >
Traceback (most recent call last):
File "blackjack.py", line 191, in <module>
main()
File "blackjack.py", line 162, in main
op = input('STAND:1, HIT:2, DOUBLE:3 > ')
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
該当のソースコード
Python3
1import random; 2def win_lose(dealer_hand, player_hand, bet, player_money): 3 player_point = get_point(player_hand) 4 dealer_point = get_point(dealer_hand) 5 if player_point <= 21: 6 if (player_point > dealer_point) or (dealer_point > 21) : 7 if player_point == 21: 8 return('<<Player WIN !!>>',player_money + int(bet*2.5)) 9 else: 10 return('<<Player WIN !!>>',player_money + bet*2) 11 elif player_point == dealer_point: 12 return('<<PUSH>>',player_money + bet) 13 else: 14 return('<<YOU LOSE>>', player_money) 15 else: 16 return('<<YOU LOSE>>', player_money) 17 18def player_op(deck, player_hand, op): 19 doubled, ending = False, False 20 if op == '1': 21 print('[PLAYER : STAND]') 22 doubled, ending = False, True 23 print(op) 24 print(doubled) 25 print(ending) 26 import pdb; pdb.set_trace() 27 elif op == '2': 28 print('[PLAYER : HIT]') 29 player_hand.append(deck.pop()) 30 print_player_hand(player_hand) 31 doubled, ending = False, False 32 elif op == '3': 33 if len(player_hand) == 2: 34 print('[PLAYER : DOUBLE]') 35 player_hand.append(deck.pop()) 36 print_player_hand(player_hand) 37 doubled, ending = True, True 38 else: 39 print('(DOUBLE CAN NOT BE ENTERED)') 40 41 if get_point(player_hand) > 21: 42 print('[PLAYER BUSTED]') 43 ending = True 44 elif get_point(player_hand) == 21: 45 print('YOUR POINT IS 21') 46 ending = True 47 48 return doubled, ending 49 50def dealer_op(deck,player_hand,dealer_hand): 51 while get_point(player_hand) <= 21: 52 if get_point(dealer_hand) >= 17: 53 print('[DEALER : STAND]') 54 break 55 else: 56 print('[DEALER : HIT]') 57 dealer_hand.append(deck.pop()) 58 print_dealer_hand(dealer_hand, False) 59 60def get_point(hand): 61 result = 0 62 ace_flag = False 63 for card in hand: 64 65 if card[RANK] == 1: 66 ace_flag = True 67 68 if card[RANK] > 10: 69 num = 10 70 else: 71 num = card[RANK] 72 result += num 73 74 if ace_flag and result <= 11: 75 result += 10 76 return result 77 78def print_player_hand(player_hand): 79 print('PLAYER(', get_point(player_hand), '): ') 80 for card in player_hand: 81 print('[', card[SUIT], card[RANK], ']') 82 print() 83 84def print_dealer_hand(dealer_hand, uncovered): 85 if uncovered: 86 print('DEALER(', get_point(dealer_hand), '): ') 87 else: 88 print('DEALER (??) : ') 89 flag = True 90 for card in dealer_hand: 91 if flag or uncovered: 92 print('[', card[SUIT], card[RANK], ']') 93 flag = False 94 print() 95 96RANK , SUIT = 0,1 97 98def make_deck(): 99 suits = ['S','H','D','C'] 100 ranks = range(1,14) 101 deck = [(x,y) for x in ranks for y in suits] 102 random.shuffle(deck) 103 return deck 104 105def main(): 106 107 turn = 1 108 player_money = 100 109 deck = make_deck() 110 111 while player_money > 0: 112 113 print('-'*20) 114 print('Turn', turn) 115 print('Amount:', player_money) 116 print('-'*20) 117 118 player_hand = [] 119 dealer_hand = [] 120 121 try: 122 bet = int(input('ベット額 >')) 123 except: 124 print('整数で入力してください') 125 continue 126 127 if bet > player_money: 128 print('所持金が不足しています') 129 elif bet <= 0: 130 print('ベットできる額は1円以上です') 131 continue 132 133 player_money -= bet 134 if len(deck) < 10: 135 deck = make_deck() 136 137 for i in range(2): 138 player_hand.append(deck.pop()) 139 dealer_hand.append(deck.pop()) 140 141 142 print('-'*20) 143 print_player_hand(player_hand) 144 print_dealer_hand(dealer_hand, False) 145 print('-'*20) 146 147 148 while True: 149 op = input('STAND:1, HIT:2, DOUBLE:3 > ') 150 doubled, ending = player_op(deck, player_hand, op) 151 if doubled: 152 player_money -= bet 153 bet += bet 154 if ending: 155 break 156 157 158 dealer_op(deck,player_hand,dealer_hand) 159 160 print('-'*20) 161 print_player_hand(player_hand) 162 print_dealer_hand(dealer_hand, True) 163 print('-'*20) 164 165 166 message,player_money = win_lose(dealer_hand,player_hand,bet,player_money) 167 print(message) 168 169 turn += 1 170 input('次のターンへ') 171 172 print('ゲームオーバー') 173 174if __name__ == '__main__': 175 main()
こちら https://teratail.storage.googleapis.com/uploads/contributed_images/56957fe805d9d7befa7dba6a98676d2b.gif を参考に、コードブロックを適用してください。また、実行環境を追記してください。
コードブロックの修正に苦戦されているようですが、<code>みたいなボタンを押して「```ここに言語を入力(改行) コード(改行) ```」を挿入→「ここに言語を入力→python」、「コード→書いたプログラム全文」に置き換えてそのままべたっと貼れば良いです。そして実行環境については、atomで走っているpythonのバージョンを確認して記載してください

回答2件
あなたの回答
tips
プレビュー