python - 函数要求输入两次,而它应该只输入一次
问题描述
我目前正在为课程作业在 Python 上开发基于文本的二十一点游戏,但是当我运行此代码时:
import assets
def show_hands(pl, dl):
print('\n' + pl.__str__())
print(f'Total: {pl.evaluate_hand()}')
print(dl.hidden_card() + '\n')
def ask_for_bet(money):
while True:
try:
bet = int(input(f'Your current amount of money is {money}, how much would you like to bet? (50 or up): '))
except ValueError:
print('That is not a number!')
else:
while bet > money or bet < 50:
if bet > money:
print("You don't have that much money!")
break
else:
print("Your bet is too low! (50 or up)")
break
else:
break
print('Alright! The game is on!')
return bet
if __name__ == '__main__':
print("-_-_-_-_-_-_-_-_-_Welcome to Omar's game of BlackJack!-_-_-_-_-_-_-_-_-_")
print('\n')
# Creating instances of both the player and the dealer, initializing the player's money
current_money = 500
player = assets.Player()
dealer = assets.Dealer()
while current_money >= 50:
# Ask for the player's bet
player_bet = ask_for_bet(current_money)
# Deal two cards to the player
print('\n' + 'The cards are being dealt...')
player.add_card(dealer.deal_cards(2))
# Show both the hand of the player and the dealer (one face down card)
show_hands(player, dealer)
# Continuously ask for the player's decision (hit/stand) as long as he chooses to hit and doesn't bust
continue_hitting = True
while continue_hitting and player.evaluate_hand() <= 21:
hit_or_stand = input('Do you want to hit or stand? (hit/stand): ').lower()
while hit_or_stand not in ('hit', 'stand'):
hit_or_stand = input('PLease input a correct value (hit/stand): ').lower()
else:
if hit_or_stand == 'stand':
continue_hitting = False
else:
player.add_card(dealer.deal_cards(1))
show_hands(player, dealer)
else:
if player.evaluate_hand() > 21:
print('You have busted!')
# Reveal the hand of the dealer and compare it with the player's
print(dealer)
这是资产模块:
import random
# -------------------------------------------------------------------------
class Player:
def __init__(self):
self.hand = []
def add_card(self, card_to_add):
self.hand += card_to_add
def evaluate_ace(self):
ace_value = input("Choose your ace's value (1/11): ")
while ace_value not in ('1', '11'):
ace_value = input("Input a correct value (1/11): ")
return int(ace_value)
def evaluate_hand(self):
hand_value = 0
for rank, suit in self.hand:
if rank.isalpha():
if rank in ('jack', 'king', 'queen'):
hand_value += 10
else:
hand_value += self.evaluate_ace()
else:
hand_value += int(rank)
return hand_value
def __str__(self):
hand_str = 'Your current hand is: '
for rank, suit in self.hand:
hand_str += f'({rank} of {suit})'
return hand_str
# -------------------------------------------------------------------------
class Dealer(Player):
suits = ['hearts', 'spades', 'diamonds', 'clubs']
ranks = ['king', 'queen', 'jack', 'ace', '2', '3', '4', '5', '6', '7', '8', '9', '10']
def __init__(self):
Player.__init__(self)
self.deck = {}
for suit in self.suits:
ranks_copy = self.ranks.copy()
random.shuffle(ranks_copy)
self.deck[suit] = ranks_copy
self.add_card(self.deal_cards(2))
def deal_cards(self, num_of_cards):
dealt_cards = []
for x in range(num_of_cards):
rand_suit = self.suits[random.randint(0, 3)]
dealt_cards.append((self.deck[rand_suit].pop(), rand_suit))
return dealt_cards
def evaluate_hand(self):
hand_value = 0
for rank, suit in self.hand:
if rank.isalpha():
if rank in ('jack', 'king', 'queen'):
hand_value += 10
else:
if hand_value > 10:
hand_value += 1
else:
hand_value += 10
else:
hand_value += int(rank)
return hand_value
def __str__(self):
hand_str = "The dealer's current hand is: "
for rank, suit in self.hand:
hand_str += f'({rank} of {suit})'
return hand_str
def hidden_card(self):
hidden_card = "The dealer's current hand is: "
for rank, suit in self.hand:
if rank != 'ace':
hidden_card += f'({rank} of {suit}) (hidden card)'
break
return hidden_card
即使他只得到一张 ace,它也会两次询问 ace 的值(如果玩家得到一张),并给出以下内容:
我尝试了不同的方法,但我仍然找不到这个问题的答案,老实说,这让我非常沮丧。
解决方案
如果您打算将某物用作值,则应将其编码为属性,而不是每次调用时都返回该值的方法。因此,如果您创建hand_value
为属性,那么您可以执行以下操作:
def add_card(self, card_to_add):
self.hand += card_to_add
self.hand_value = evaluate_hand()
然后你可以用 替换所有的实例self.evaluate_hand()
,self.hand_value
并且玩家只会被提示一次王牌值,当self.hand_value
被设置时。
另外,需要考虑的一些事项:
当你抽一张牌时,你同样有可能抽到任何花色。因此,如果甲板上还剩下 10 颗钻石和 1 颗心,那么您的算法抽到心的概率与所有钻石加在一起的概率一样多。
您实际上不需要向玩家询问 ace 值;显然,如果玩家可以在不破坏的情况下选择 11,则玩家将选择 1,否则选择 1。我在赌场没有太多经验。荷官真的会问玩家他们希望他们的 A 有什么价值吗?您应该在没有 A 的情况下计算手牌价值,然后执行max(rest_of_hand+number_of_aces+10*i for i in range(number_of_aces) if rest_of_hand+number_of_aces+10*i < 22)
,并将结果作为手牌价值(请注意,您必须确保这永远不会导致尝试获取max
空集,但这不应该发生只要玩家的手牌值达到 21 或更高,手牌就终止)。
推荐阅读
- r - 如何将此 dplyr 语句转换为 R 函数
- python - dynamodb 从列表中删除特定项目而不会发生冲突
- javascript - 如何访问 javascript/typescript 中的重复计数
- ios - iOS 12 致命异常:NSInternalInconsistencyException
- angularjs - 无法加载 ui-router 嵌套状态页面
- ios - 即使应用程序在前台运行,也会调用通知 - iOS12
- reactjs - 使用 redux 仅在 React 组件中获取部分存储/状态的最佳实践
- websocket - NestJS websocket 向客户端广播事件
- angular - Angular DevExtreme 主题
- ruby-on-rails - Ruby on Rails - RSpec 测试随机失败