我遇到的主要问题是当我选择 stay
时会发生什么上hand_one
,然后 hit
上hand_two
.
而不是让我hit or stay
上hand_two
再次,它让我回到hit or stay
上hand_one
,当我已经选择留在 hand_one 时,所以 hand_one 应该没有更多选择。这会导致出现多个打印语句和不正确的游戏的问题。
我的代码有什么问题,就像导致它循环回到 hand_one
.
完整代码在这里:http://labs.codecademy.com/Bjaf/2#:workspace
这是可能导致问题的部分。
def hit_or_stay(person):
hit_or_stay = raw_input("Do you want to hit or stay? You can type h or s.")
if hit_or_stay == 'h' or hit_or_stay == 'hit':
deal_one_card(person)
value_of_current_cards(person)
number_value_of_hand()
elif hit_or_stay == 's'or hit_or_stay == 'stay':
print "You stayed"
return
else:
hit_or_stay(person)
def number_value_of_hand():
if number_of_hands > 0:
value_of_hand_one = value_of_current_cards(hand_one)
if value_of_hand_one < 18:
print "\n" "You have %i" % (value_of_hand_one)
hit_or_stay(hand_one)
elif value_of_hand_one > 18:
print "You Lose"
return
elif value_of_hand_one == 18:
print "You hit HOT 18!!!"
return
if number_of_hands > 1:
value_of_hand_two = value_of_current_cards(hand_two)
if value_of_hand_two < 18:
print "\n" "Your second hand has %i" % (value_of_hand_two)
hit_or_stay(hand_two)
elif value_of_hand_two > 18:
print "You Lose"
return
elif value_of_hand_two == 18:
print "You hit HOT 18!!!"
return
number_value_of_hand()
谁能明白为什么它会循环回来给hand_one另一个选择?以及我该如何解决它?非常感谢!
最佳答案
您的问题出现在这一步:
hit_or_stay(hand_two)
当你点击 hand_two 时,你的代码会执行以下操作:
deal_one_card(person)
value_of_current_cards(person)
number_value_of_hand()
问题就在那里,因为 number_value_of_hand()
将您带回到该函数的开头,并再次遍历 hand_one 选项。
您可能需要重写 number_value_of_hand()
函数以包含一个参数,告诉它从哪里开始(hand_one、hand_two 等)
我可能会创建一个手牌列表,并遍历该列表。然后,您可以调用 number_of_hands(hands[i])
来获取第 i
手。
关于python - Blackjack 风格的 Python(计算机)游戏循环不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20481181/