-6

Im working with python 3.6 and I wrote this:

zen = 1
zen2 = True
def test():
    if zen == 1 and zen2 == True:
        print ("hello")
        global zen2
        zen2 = False
    else:
        print ("hello 2")

test()

...and the output is NOTHING! NADA! No "hello" nor "hello 2", not even an error!

Even if I do this:

zen = 1
zen2 = True
def test():
    if zen == 1 :
        if zen2 == True:
            print ("hello")
            global zen2
            zen2 = False
        else:
            pass
    else:
        print ("hello 2")

test()

Still NOTHING! Why?

Edit: I'm sorry everyone, before this I didn't notice the "else" does not have a colon,so for some they got a Syntax error, its probably fixed now,but it still outputs nothing

Edit 2: this is going to be embarrassing, as since i was using PyCharm and my other file is open (in the file, no function is executed), i accidentally ran the other file rather than the this "zen" file,so i got nothing because it. I actually got a SyntaxError from this "zen" file and this post really helped me with the problem even though I stated that was not the problem, and it also reminded myself to double check everything before doing anything, so yeah, thanks for anyone who helped me :)

Jazz Handy
  • 15
  • 2
  • 1
    Your code as it sits does not run. – dfundako Oct 30 '18 at 15:11
  • 5
    Cannot reproduce. This prints "hello" after issuing a syntax warning. – Carcigenicate Oct 30 '18 at 15:13
  • Hello, @JazzHandy in order for us to help you we need an example that reproduces the behavior you are seeing. In this case when I run the code, the effect is just as you expect: https://ideone.com/eE50Lw – Ivaylo Strandjev Oct 30 '18 at 15:15
  • IIRC in Python2, the `zen` and `zen2` uses in `test()` would be taken as global variables. In Python3, they're local variables unless you specifically make them global. You're using `global`, but too late and in the wrong place (after the condition). So the local `zen` and `zen2` both get the default value of `None`. –  Oct 30 '18 at 15:16
  • 2
    I'm surprised there are a few different comments on whether this runs. I get: `SyntaxError: name 'zen2' is used prior to global declaration`. @JazzHandy this is definitely a scope issue though. – r.ook Oct 30 '18 at 15:16
  • @Idlehands In QPython on my phone, I get the same message, but it's a `SyntaxWarning`, not an error, and it still prints. Odd. – Carcigenicate Oct 30 '18 at 15:18
  • @Idlehands - that might also depend on the Python version - I don't know for sure, but it makes sense that they might have added the check some time after adding `global` –  Oct 30 '18 at 15:19
  • Possible duplicate of [Why does python behave this way with variables?](https://stackoverflow.com/questions/48192290/why-does-python-behave-this-way-with-variables) Here's my previous answer on a similar scoping issue. – r.ook Oct 30 '18 at 15:25

3 Answers3

0

Solution 1: Pass the arguments in the function.

Solution 2: Declare "zen" and "zen2" as global before declaration.

Adrish
  • 52
  • 1
  • 7
0

You have some logical errors. You can not use zend2 before using global. You should pass variables as arguments or declare them as global at first line of function.

-1

I assume this is what you wanted to achive:

def test(zen, zen2):
    if zen == 1 and zen2 == True:
        print ("hello")
        zen2 = False
    else:
        print ("hello 2")

test(1, True)

As some comments suggested, if one tries to run your code, it returns following (on linux GCC 4.8.2/Python 3.6.1):

SyntaxError: name 'zen2' is used prior to global declaration

In case you want to change the value printed after the first evaluation of the condition, rewrite it like this:

def test(zen, zen2):
    if zen == 1 and zen2 == True:
        print ("hello")
        zen2 = False
        test(zen, zen2)
    else:
        print ("hello 2")

test(1, True)
New2coding
  • 715
  • 11
  • 23
  • I don't think this will do precisely what they want. `zen2 = False` suggests that they want subsequent calls to behave differently. Reassigning the `zen2` parameter however won't have any effect. – Carcigenicate Oct 30 '18 at 15:28