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

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

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

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

1253閲覧

python ブラックジャック

aiueooii

総合スコア0

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/06/19 13:04

前提・実現したいこと

(例)pythonでブラックジャックを作りたいと考えています。
100戦行い勝敗を確認する際にこのようなエラーが起きました。どのように変更すればよいか教えてくださるとありがたいです(完成させたいです)。(エラーの内容等も教えてくださるとありがたいです。

発生している問題・エラーメッセージ

Traceback (most recent call last): File "Main.py", line 74, in <module> hit(player2) File "Main.py", line 33, in hit draw_num = deck.pop(random.randint(0, len(deck)-1)) File "/usr/lib/python3.8/random.py", line 248, in randint return self.randrange(a, b+1) File "/usr/lib/python3.8/random.py", line 226, in randrange raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width)) ValueError: empty range for randrange() (0, 0, 0)

該当のソースコード

python3

1import random 2suits=["spade", "heart", "diamond", "clover"] 3nums=["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"] 4deck = [] 5for suit in suits: 6 for num in nums: 7 deck.append((suit, num)) 8player1_total = [] 9player2_total = [] 10player1="player1" 11player2="player2" 12 13def showdown(): 14 player1_point = convert(player1_total) 15 player2_point = convert(player2_total) 16 if 21 < player1_point and 21 < player2_point: 17 return 0 18 elif 21 < player1_point: 19 return 2 20 elif 21 < player2_point: 21 return 1 22 elif player2_point < player1_point: 23 return 1 24 elif player1_point < player2_point: 25 return 2 26 elif player1_point == player2_point: 27 return 0 28 else: 29 assert False, f"player1:{player1_point} player2:{player2_point}" 30 31def hit(user): 32 draw_num = deck.pop(random.randint(0, len(deck)-1)) 33 if user=="player1": 34 player1_total.append(draw_num) 35 if user=="player2": 36 player2_total.append(draw_num) 37 38def convert(cards): 39 point = 0 40 a_num = 0 41 for card in cards: 42 number = card[1] 43 if number=="A": 44 a_num += 1 45 elif number=="J" or number=="Q" or number=="K": 46 point += 10 47 else: 48 point += int(number) 49 for i in range(a_num): 50 if 21 < point + 11: 51 point += 1 52 else: 53 point += 11 54 return point 55 56 57game_num = 100 58results = [] 59for i in range(game_num): 60 player1_stay = False 61 player2_stay = False 62 while player1_stay is False or player2_stay is False: 63 if player1_stay is False: 64 hit(player1) 65 if 2 <= len(player1_total): 66 player1_stay = True 67 if player2_stay is False: 68 hit(player2) 69 if 2 <= len(player2_total): 70 player2_stay = True 71result = showdown() 72results.append(result) 73 74if result == 0: 75 print("Draw") 76else: 77 print(f"Player{result} Win!!") 78print(f"player score :{convert(player1_total)}, cards:{player1_total}") 79print(f"player2 score :{convert(player2_total)}, cards:{player2_total}")

試したこと

deckとplayerの手札もリセットする必要があるかと思い、deck_origin = []
などを用いて別のリストも作りましたがエラーが同様に起きました。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

deckが空になったからでしょう。
引くカードが無くなったらどうするかを決めて処理を追加しましょう。

投稿2021/06/19 13:11

otn

総合スコア84566

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問