前提・実現したいこと
(例)python3でブラックジャックを作っています。
対戦結果を100回表示させるよう(任意の数)に変更したところ、実装中に以下のエラーメッセージが発生しました。エラーの意味は分かっていますが、どこを直せばよいかが色々変えてみましたがわかりません。教えてくださると幸いです。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "Main.py", line 64, in <module> hit(player1) File "Main.py", line 32, in hit draw_num = deck.pop(random.sample(len(deck))) TypeError: sample() missing 1 required positional argument: 'k'
該当のソースコード
import random suits=["spade", "heart", "diamond", "clover"] nums=["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"] deck = [] for suit in suits: for num in nums: deck.append((suit, num)) player1_total = [] player2_total = [] player1="player1" player2="player2" def showdown(): player1_point = convert(player1_total) player2_point = convert(player2_total) if 21 < player1_point and 21 < player2_point: return 0 elif 21 < player1_point: return 2 elif 21 < player2_point: return 1 elif player2_point < player1_point: return 1 elif player1_point < player2_point: return 2 elif player1_point == player2_point: return 0 else: assert False, f"player1:{player1_point} player2:{player2_point}" def hit(user): draw_num = deck.pop(random.sample(len(deck))) if user=="player1": player1_total.append(draw_num) if user=="player2": player2_total.append(draw_num) def convert(cards): point = 0 a_num = 0 for card in cards: number = card[1] if number=="A": a_num += 1 elif number=="J" or number=="Q" or number=="K": point += 10 else: point += int(number) for i in range(a_num): if 21 < point + 11: point += 1 else: point += 11 return point game_num = 100 results = [] for i in range(game_num): player1_stay = False player2_stay = False while player1_stay is False or player2_stay is False: if player1_stay is False: hit(player1) if 2 <= len(player1_total): player1_stay = True if player2_stay is False: hit(player2) if 2 <= len(player2_total): player2_stay = True result = showdown() results.append(result) if result == 0: print("Draw") else: print(f"Player{result} Win!!") print(f"player score :{convert(player1_total)}, cards:{player1_total}") print(f"player2 score :{convert(player2_total)}, cards:{player2_total}")
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/06/19 00:20