Python: Code is ignoring the if and else

Terry Reedy tjreedy at udel.edu
Fri Aug 2 21:42:30 EDT 2013


Nonsense: they are executed just as you ask, even though what you ask is 
not what you meant.

On 8/2/2013 8:40 PM, kevin4fong at gmail.com wrote:

> def player_0_hitman(hit):
>      for card in pHands[target_player]:
>          if target_card[0] == card[0]:
>              count = pHands[target_player].count(card)
>              pHands[0].append(card)
>              pHands[target_player].remove(card)
>              ShowMessage("HIT: " + str(count) + " card(s) transferred")
>      else:
            else: # indent to match if
>          if target_card[0] != card[0]:
            # delete this if line, see below.

>              top_card = GetTopCard(sDeck)
>              pHands[0].append(top_card)
>              if top_card[0] == target_card[0]:
>                  ShowMessage("HIT: LUCKILY Player 0 has fished up a rank <" + str(top_card[0]) + ">!!!")
>              else:
>                  ShowMessage("MISS: You fished up the rank <" + str(top_card[0]) + ">")
>                  hit = hit - 1
>              return hit

This executes the for loop multiple times and the the else: clause of 
the *for* statement (see the ref manual). I believe you want the whole 
else: clause indented so that it will be executed when the if condition 
is false. If so, the second if is redundant and should just be removed.

-- 
Terry Jan Reedy




More information about the Python-list mailing list