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


Groups > comp.lang.python > #51839

Re: Python: Code is ignoring the if and else

References <c6702eb3-494a-4359-84ef-d6679c09df35@googlegroups.com> <dfa6368b-565b-4512-8755-ef36fd2719cd@googlegroups.com> <b16bb894-a720-4e0c-a3ce-d699afffeb8e@googlegroups.com>
From Joshua Landau <joshua@landau.ws>
Date 2013-08-03 03:11 +0100
Subject Re: Python: Code is ignoring the if and else
Newsgroups comp.lang.python
Message-ID <mailman.142.1375495945.1251.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 3 August 2013 02:44, <kevin4fong@gmail.com> wrote:

> Yeah, I already know about that. But if I try to change it, I'm not even
> able to start the program. If I try to change the if statement that it
> corresponds with, I get a an error saying "card" is not a global. And if I
> try to shift it in, for some reason...the program runs through the MISS
> line multiple times.
>

You have a car with a broken engine and a broken tire and are telling us
that you refuse to fix the engine because it highlights the problem of the
broken tire.

Take the fix and move on to the next problem.

One piece of advice is about scoping. This is perhaps the hardest "gotcha"
of Python conceptually, but it's sensible once you understand the
justifications.

Run the four commands below and try and understand why this applies to your
code.

a = 1

def access_global():
    print(a)

def set_variable():
    a = 2
    print(a)

def broken_set():
    a = a + 1
    print(a)

def also_broken():
    print(a)
    return
    a = 1 # Never run!

The fix for the broken variants is to start the functions with "global a".

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