Question

The major problem I'm having is what occurs when I choose stay on hand_one, and then hit on hand_two.

Instead of asking me to hit or stay on hand_two again, it brings me back to hit or stay on hand_one, when I already chose stay on hand_one, so hand_one should have no more options. This causes issues with multiple print statements occurring and incorrect game play.

What is wrong with my code that it is like causing it to loop back to hand_one.
The full code is here: http://labs.codecademy.com/Bjaf/2#:workspace

Here is the part likely causing the issue.

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

Can anyone see why it loops back to give hand_one another option? And possibly how I can fix it? Thanks so much!

Was it helpful?

Solution

Your problem occurs on this step:

hit_or_stay(hand_two)

When you hit on hand_two, your code does this:

deal_one_card(person)
value_of_current_cards(person)
number_value_of_hand()

The problem is right there, because number_value_of_hand() brings you back to the beginning of that function, and goes through the hand_one options again.

You will probably have to rewrite your number_value_of_hand() function to include an argument that tells it where to begin (hand_one, hand_two, etc.)

I would probably make a list of hands, and iterate through the list. Then, you could call number_of_hands(hands[i]) to being at the ith hand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top