Hi,

A simple question I think.

I want to create a function that updates variables if a certain condition is met.

For example

def main():
c=d=e=0
if c[1] == 0:
c=c+1
d=d+1
e=e+1

I want to create a function that adds the 1 to c,d,e automatically.

def BB(c,d,e)
Add 1 to c
Add 1 to d
Add 1 to e

Any help is appreciated.
macca111

Just an exercise in passing arguments to functions:

def increment(a, b, c):
    a += 1
    b += 1
    c += 1
    return a, b, c

def main():
    c = d = e = 0
    c, d, e = increment(c, d, e)
    # test the result
    print c, d, e  # 1 1 1
main()

Just an exercise in passing arguments to functions:

def increment(a, b, c):
    a += 1
    b += 1
    c += 1
    return a, b, c
 
def main():
    c = d = e = 0
    c, d, e = increment(c, d, e)
    # test the result
    print c, d, e  # 1 1 1
main()

Hi,

What if I only need to increase a or b not all of them?

macca1111

To just increase on variable such as "a" use this code in your main function:

a += 1

This is shorthand for a = a + 1.

We just discussed that in one of sneekula's threads:

# exercise in argument passing

def increment(*args):
    """receives tuple *args, change to list to process, then back to tuple"""
    args = list(args)
    index = 0
    for arg in args:
        args[index] += 1
        index += 1
    return tuple(args)
        

def main():
    c = d = e = 0

    # make sure you match arguments
    c, d, e = increment(c, d, e)
    print c, d, e   # 1 1 1

    c, d = increment(c, d)
    print c, d  # 2 2
    
    c, = increment(c)
    print c  # 3 


main()

We just discussed that in one of sneekula's threads:

# exercise in argument passing

def increment(*args):
    """receives tuple *args, change to list to process, then back to tuple"""
    args = list(args)
    index = 0
    for arg in args:
        args[index] += 1
        index += 1
    return tuple(args)
        

def main():
    c = d = e = 0

    # make sure you match arguments
    c, d, e = increment(c, d, e)
    print c, d, e   # 1 1 1

    c, d = increment(c, d)
    print c, d  # 2 2
    
    c, = increment(c)
    print c  # 3 


main()

Thks, exactly what I needed.

Maybe you could let me know why the following doesn't work.

If I do

ch =

then put in an if statement

if ch[1]==1 and ch[2:5]==0:
#add 1 to c and d

At present it doesn't recognise the second if statement and won't increase the variables.

thks in advance
macca1111

This question has already been answered. Start a new discussion instead.