二十一点风格蟒蛇(电脑)游戏循环不正确

问题描述:

我遇到的主要问题是当我选择stayhand_one,然后hithand_two发生什么。二十一点风格蟒蛇(电脑)游戏循环不正确

不要问我hit or stayhand_two再次,它使我回hit or stayhand_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,你的代码做这个:在这一步发生

+1

hit_or_stay调用number_value_of_hand,它将执行行放回到number_value_of_hand的开头。 – M4rtini

+2

这似乎是一个典型的代码示例,在类中实现会更容易 – asdf

+1

在hit_or_call函数中。为hand1 \ hand2添加一个参数。将它传递给number_value_of_hand并用它来检查哪一手继续。 – M4rtini

你的问题

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等)

,我可能会做一个手的list,并遍历列表。然后,您可以拨打number_of_hands(hands[i])以获得第i号手。