How do I update the counter when using function in while loop? It keep gives me 2
def Main(Counter):
Counter = Counter +1
print (Counter)
A = 1
while True:
Main (A)
How do I update the counter when using function in while loop? It keep gives me 2
def Main(Counter):
Counter = Counter +1
print (Counter)
A = 1
while True:
Main (A)
As you are passing the same value of A
each time in Main()
function, it prints the same value 2
. To increase the counter
you need to increment it in while loop
and not in function itself.
def Main(Counter):
#Counter = Counter +1
print (Counter)
A = 1
while True:
Main(A)
A += 1
If you want to increment the value inside the function for some reason, you can use return
keyword to return the incremented value and store it in the variable.
def Main(Counter):
Counter = Counter +1
print (Counter)
return Counter
A = 1
while True:
A = Main(A)