質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

2615閲覧

unexpected EOF while parsing のエラーが消せません

songyong

総合スコア21

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/04/14 14:54

編集2018/04/14 15:37

プログラミング初心者です。以下のソースですが、どこに問題があるかわかりません。助けてください。

ブラックジャックゲームを作成しています。
実行時にエラーが発生します。スタンドかヒットかダブルかをインプットで数字を入力したあと、後続の処理に進みません。

環境は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()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hayataka2049

2018/04/14 15:24 編集

コードブロックの修正に苦戦されているようですが、<code>みたいなボタンを押して「```ここに言語を入力(改行) コード(改行) ```」を挿入→「ここに言語を入力→python」、「コード→書いたプログラム全文」に置き換えてそのままべたっと貼れば良いです。そして実行環境については、atomで走っているpythonのバージョンを確認して記載してください
guest

回答2

0

ベストアンサー

手元環境(python3.5.1)では再現しませんでした。
SyntaxError: unexpected EOF while parsingというエラーメッセージが出たとのことですが、同様の症状のケースを見つけました。

Python - SyntaxError: unexpected EOF while parsingのエラーメッセージがわからない(107226)|teratail

ただしこれはpython3用のスクリプトをpython2で実行した場合の症状で、本当にpython3.6.5で実行できていれば考えられない症状です。
atomの設定に不備があってpython2で実行してしまっている可能性があるので、まずはmainの先頭に以下のコードを入れてバージョンを確認してください。

python

1import sys 2 3print(sys.version)

投稿2018/04/14 15:49

hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

songyong

2018/04/14 15:59

ありがとうございます。おっしゃる通りPython2が動作しておりました。設定の変更をやってみます。
guest

0

Python2を誤って起動しているような気がします。
コードの最初の行にimport sys; print(sys.version)と入力して実行してみてください。

投稿2018/04/14 15:48

LouiS0616

総合スコア35660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

songyong

2018/04/14 16:01

迅速なご回答、ありがとうございます。助かりました。また、是非ご助言いただければと思います。質問の仕方からご教示いただき、本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問