Home > Blockchain >  counting machine with function for 1 variable
counting machine with function for 1 variable

Time:08-01

So, I've been trying to make a counting machine to a variable n. I'm using a function to get it done yet it keeps stating that n=0 which I've used at the beginning of the code. This is what I'm using.

n=0

def cprocess():
    n=n 1

It's I guess, pretty simple, but I am a beginner. How do I improve & fix the code?

CodePudding user response:

You never call the function, a function only executes code when called. Also, don't use the syntax x=x 1, instead use the = operator.

Since you seem new, I'll go step by step:

n=0 # defining our integer variable

def cprocess(): # defining our function
    global n # leave this out if you want, functions and variables tend to be weird sometimes, so by doing this we make sure variable "n" exists
    n  = 1 # incrementing variable "n" by 1

cprocess() # calling our function

print(n) # checking our variable
>>> 1 # output of print statement

CodePudding user response:

Add global n inside the function.

n=0

def cprocess():
    global n
    n=n 1
  • Related