1

我遇到的主要问题是当我选择stayon hand_one,然后hiton时会发生什么hand_two

hit or stay当我已经选择留在 hand_one 上时,它没有让我再次打开,而是hand_two让我重新hit or stay打开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 另一个选择?可能我该如何解决?非常感谢!

4

1 回答 1

2

您的问题出现在这一步:

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

我可能会list举手,并遍历列表。然后,你可以打电话number_of_hands(hands[i])给他i

于 2013-12-09T21:47:03.157 回答