-5

When I put this in it brings up an error and I'd like some help finding out what I did wrong.

x = 0

def func(var):  
    if var = True:  
        x = x - 1
    else:
        pass

The error comes from x = x - 1, and I can't figure out what I'm doing wrong.

3 Answers3

4

In order to use x inside the scope of the function you have to tell the compiler where to find x so change your code to:

x = 0
def func(var):  
    if var == True:
        global x  # look for x outside the scope of the function
        x = x - 1
    else:
        pass

Also it's nice to have a return into functions so I propose:

x = 0
def func(var):  
    if var == True:
        global x  # look for x outside the scope of the function
        x = x - 1
    return None  # or -> return x (to return the value of x)
georstef
  • 1,368
  • 3
  • 12
  • 18
  • there is no reason to return None, python returns None by default. If you need an early return with no value just return with no value, this is python's equivalent of a void function. Although it not good for chaining good python style discourages functions that mutate and return values. – Roman A. Taycher May 03 '14 at 10:56
  • Thanks for helping an idiot like me. – user3598814 May 03 '14 at 11:09
2

The error is actually from the line before. You are confusing = (assignment operator) with the == (comparison operator). The correct code is as follows:

def func(var):
    global x
    if var == True:  
        x -= 1         #also you can simply do it as x -= 1 instead of x = x - 1
    else:
        pass

Finally, there is really no point in having else: pass since you're not doing anything in there. Just shorten your code to:

def func(var):
    global x
    if var == True:
        x -= 1
sshashank124
  • 31,495
  • 9
  • 67
  • 76
0

First you put an assignment operator = instead of the equals operator ==

x = 0

def func(var):  
    if var == True:  
        x = x - 1
    else:
        pass

Second never check if expressions/values are equal(==) to True /False especially in python. Just give a statement/variable that is True or truthy/False or falsey. In python None(null) empty collections such as [], 0, 0.0, timedelta of zero, and False are falsey and just about everything else is truthy. To test this for yourself just try passing the value or expression to the bool function to see if it returns True or False. See https://docs.python.org/2/library/stdtypes.html#truth-value-testing for more info.

x = 0

def func(var):  
    if var:  
        x = x - 1
    else:
        pass

Third you can lookup but not reassign a variable declared in the same module outside the function. unless you declare it global. so you can print without global

x = 0

def func(var):  
    if var:
        print x

but not reassign it. Also you don't need a else branch or return statement(python defaults to returning None), so it's just

x = 0

def func(var):  
    if var:
        global x  
        x = x - 1
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141