Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51835

Re: Python: Code is ignoring the if and else

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Python: Code is ignoring the if and else
Date 2013-08-02 21:42 -0400
References <c6702eb3-494a-4359-84ef-d6679c09df35@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.140.1375494162.1251.python-list@python.org> (permalink)

Show all headers | View raw


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@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python: Code is ignoring the if and else kevin4fong@gmail.com - 2013-08-02 17:40 -0700
  Re: Python: Code is ignoring the if and else MRAB <python@mrabarnett.plus.com> - 2013-08-03 02:38 +0100
  Re: Python: Code is ignoring the if and else John Ladasky <john_ladasky@sbcglobal.net> - 2013-08-02 18:39 -0700
    Re: Python: Code is ignoring the if and else kevin4fong@gmail.com - 2013-08-02 18:44 -0700
      Re: Python: Code is ignoring the if and else Chris Angelico <rosuav@gmail.com> - 2013-08-03 02:56 +0100
      Re: Python: Code is ignoring the if and else Joshua Landau <joshua@landau.ws> - 2013-08-03 03:11 +0100
        Re: Python: Code is ignoring the if and else kevin4fong@gmail.com - 2013-08-02 19:24 -0700
          Re: Python: Code is ignoring the if and else Terry Reedy <tjreedy@udel.edu> - 2013-08-03 01:04 -0400
            Re: Python: Code is ignoring the if and else kevin4fong@gmail.com - 2013-08-03 13:12 -0700
  Re: Python: Code is ignoring the if and else Terry Reedy <tjreedy@udel.edu> - 2013-08-02 21:42 -0400
    Re: Python: Code is ignoring the if and else kevin4fong@gmail.com - 2013-08-02 18:46 -0700
  Re: Python: Code is ignoring the if and else Dave Angel <davea@davea.name> - 2013-08-03 03:34 +0000

csiph-web