Python: Code is ignoring the if and else

Joshua Landau joshua at landau.ws
Fri Aug 2 22:11:37 EDT 2013


On 3 August 2013 02:44, <kevin4fong at 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".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130803/d23f9a64/attachment.html>


More information about the Python-list mailing list