プログラミング初心者です。以下のソースですが、どこに問題があるかわかりません。助けてください。
ブラックジャックゲームを作成しています。
実行時にエラーが発生します。スタンドかヒットかダブルかをインプットで数字を入力したあと、後続の処理に進みません。
環境は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
該当のソースコード
import random;
def win_lose(dealer_hand, player_hand, bet, player_money):
player_point = get_point(player_hand)
dealer_point = get_point(dealer_hand)
if player_point <= 21:
if (player_point > dealer_point) or (dealer_point > 21) :
if player_point == 21:
return('<<Player WIN !!>>',player_money + int(bet*2.5))
else:
return('<<Player WIN !!>>',player_money + bet*2)
elif player_point == dealer_point:
return('<<PUSH>>',player_money + bet)
else:
return('<<YOU LOSE>>', player_money)
else:
return('<<YOU LOSE>>', player_money)
def player_op(deck, player_hand, op):
doubled, ending = False, False
if op == '1':
print('[PLAYER : STAND]')
doubled, ending = False, True
print(op)
print(doubled)
print(ending)
import pdb; pdb.set_trace()
elif op == '2':
print('[PLAYER : HIT]')
player_hand.append(deck.pop())
print_player_hand(player_hand)
doubled, ending = False, False
elif op == '3':
if len(player_hand) == 2:
print('[PLAYER : DOUBLE]')
player_hand.append(deck.pop())
print_player_hand(player_hand)
doubled, ending = True, True
else:
print('(DOUBLE CAN NOT BE ENTERED)')
if get_point(player_hand) > 21:
print('[PLAYER BUSTED]')
ending = True
elif get_point(player_hand) == 21:
print('YOUR POINT IS 21')
ending = True
return doubled, ending
def dealer_op(deck,player_hand,dealer_hand):
while get_point(player_hand) <= 21:
if get_point(dealer_hand) >= 17:
print('[DEALER : STAND]')
break
else:
print('[DEALER : HIT]')
dealer_hand.append(deck.pop())
print_dealer_hand(dealer_hand, False)
def get_point(hand):
result = 0
ace_flag = False
for card in hand:
if card[RANK] == 1:
ace_flag = True
if card[RANK] > 10:
num = 10
else:
num = card[RANK]
result += num
if ace_flag and result <= 11:
result += 10
return result
def print_player_hand(player_hand):
print('PLAYER(', get_point(player_hand), '): ')
for card in player_hand:
print('[', card[SUIT], card[RANK], ']')
print()
def print_dealer_hand(dealer_hand, uncovered):
if uncovered:
print('DEALER(', get_point(dealer_hand), '): ')
else:
print('DEALER (??) : ')
flag = True
for card in dealer_hand:
if flag or uncovered:
print('[', card[SUIT], card[RANK], ']')
flag = False
print()
RANK , SUIT = 0,1
def make_deck():
suits = ['S','H','D','C']
ranks = range(1,14)
deck = [(x,y) for x in ranks for y in suits]
random.shuffle(deck)
return deck
def main():
turn = 1
player_money = 100
deck = make_deck()
while player_money > 0:
print('-'*20)
print('Turn', turn)
print('Amount:', player_money)
print('-'*20)
player_hand = []
dealer_hand = []
try:
bet = int(input('ベット額 >'))
except:
print('整数で入力してください')
continue
if bet > player_money:
print('所持金が不足しています')
elif bet <= 0:
print('ベットできる額は1円以上です')
continue
player_money -= bet
if len(deck) < 10:
deck = make_deck()
for i in range(2):
player_hand.append(deck.pop())
dealer_hand.append(deck.pop())
print('-'*20)
print_player_hand(player_hand)
print_dealer_hand(dealer_hand, False)
print('-'*20)
while True:
op = input('STAND:1, HIT:2, DOUBLE:3 > ')
doubled, ending = player_op(deck, player_hand, op)
if doubled:
player_money -= bet
bet += bet
if ending:
break
dealer_op(deck,player_hand,dealer_hand)
print('-'*20)
print_player_hand(player_hand)
print_dealer_hand(dealer_hand, True)
print('-'*20)
message,player_money = win_lose(dealer_hand,player_hand,bet,player_money)
print(message)
turn += 1
input('次のターンへ')
print('ゲームオーバー')
if __name__ == '__main__':
main()
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
+2
手元環境(python3.5.1)では再現しませんでした。
SyntaxError: unexpected EOF while parsingというエラーメッセージが出たとのことですが、同様の症状のケースを見つけました。
Python - SyntaxError: unexpected EOF while parsingのエラーメッセージがわからない(107226)|teratail
ただしこれはpython3用のスクリプトをpython2で実行した場合の症状で、本当にpython3.6.5で実行できていれば考えられない症状です。
atomの設定に不備があってpython2で実行してしまっている可能性があるので、まずはmainの先頭に以下のコードを入れてバージョンを確認してください。
import sys
print(sys.version)
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
+2
Python2を誤って起動しているような気がします。
コードの最初の行にimport sys; print(sys.version)
と入力して実行してみてください。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.32%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
質問への追記・修正、ベストアンサー選択の依頼
LouiS0616
2018/04/15 00:00
こちら https://teratail.storage.googleapis.com/uploads/contributed_images/56957fe805d9d7befa7dba6a98676d2b.gif を参考に、コードブロックを適用してください。また、実行環境を追記してください。
hayataka2049
2018/04/15 00:24 編集
コードブロックの修正に苦戦されているようですが、<code>みたいなボタンを押して「```ここに言語を入力(改行) コード(改行) ```」を挿入→「ここに言語を入力→python」、「コード→書いたプログラム全文」に置き換えてそのままべたっと貼れば良いです。そして実行環境については、atomで走っているpythonのバージョンを確認して記載してください