How to use variables together with functions (2)

Asked by chaz7201

I'm just looking for an answer on how to count with a passable variable.
####################### This always returns 0 inside the loop ##
a = 0
def addOne(a):
    a +=1
while exists("1316568059480.png"): ## just made this to control the loop.
    addOne(a)
    popup(str(a)) ## need this to count up by one each click.
##################################################

########## This works as a counter but is useless##############
a = 0
while exists("1316568059480.png"): ## just made this to control the loop.
    a +=1
    popup(str(a)) ## need this to count up by one each click.
##################################################

#### This works just the same as the first example ##############
def program():
        a = 0
    def countOne(a):
        a +=1
    while exists("1316568059480.png"): ## just made this to control the loop.
        countOne(a)
        popup(str(a)) ## need this to count up by one each click.
program()
#################################################

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Dude
Solved:
Last query:
Last reply:
Revision history for this message
Best Dude (dhurga-devi) said :
#1

Hi,

In ur code off course u will get zero, u r assigning a=0 , and u r calling that function countOne(a):
, but u r not returning any value inside that function. by default it assign a as zero.

This code might help to solve ur problem

def addOne(a):
    a +=1
    return a

count = 0
while exists("C:\images\icon1.png",30):
    count = addOne(count)
    popup(str(count))

Revision history for this message
chaz7201 (chaz7201) said :
#2

Thank you sooo much.