3

I'm trying to increase the count of an integer given that an if statement returns true. However, when this program is ran it always prints 0.I want n to increase to 1 the first time the program is ran. To 2 the second time and so on.

I know functions, classes and modules you can use the global command, to go outside it, but this doesn't work with an if statement.

n = 0
print(n)

if True:
    n += 1
1
  • Remember that Python executes each line of code in order from top to bottom. Commented Mar 2, 2018 at 4:46

3 Answers 3

5

Based on the comments of the previous answer, do you want something like this:

n = 0
while True:
    if True: #Replace True with any other condition you like.
        print(n)
        n+=1   

EDIT:

Based on the comments by OP on this answer, what he wants is for the data to persist or in more precise words the variable n to persist (Or keep it's new modified value) between multiple runs times.

So the code for that goes as(Assuming Python3.x):

try:
    file = open('count.txt','r')
    n = int(file.read())
    file.close()
except IOError:
    file = open('count.txt','w')
    file.write('1')
    file.close()
    n = 1
print(n)

n += 1

with open('count.txt','w') as file:
    file.write(str(n))
 print("Now the variable n persists and is incremented every time.")
#Do what you want to do further, the value of n will increase every time you run the program

NOTE: There are many methods of object serialization and the above example is one of the simplest, you can use dedicated object serialization modules like pickle and many others.

Sign up to request clarification or add additional context in comments.

2 Comments

this increase the count without me running the program again. I have to exit the IDE to terminate it. I want to be able to increase the count by one each time it's ran. I'm not sure how a while or for loop could be used here. Any additional help would be appreciated.
You cannot store a variable between multiple runs of your program in a conventional way, what you need is called Persisting Data, for that, you'll have to write the variable n's value to a file and then everytime you run it , it reads the value of n from that file (which is stored on your local drive) , increment it by one and then write it back again to the file. I'll edit the code for that.
1

If you want it to work with if statement only. I think you need to put in a function and make to call itself which we would call it recursion.

def increment():
    n=0
    if True:
        n+=1
        print(n)
        increment()
increment()

Note: in this solution, it would run infinitely. Also you can use while loop or for loop as well.

4 Comments

If I need it to run only once each time the program is ran is there a way to do this without using if statements?
Do you mean if you run the program it will increase the n? I did not get what you mean.
If I click run on the program the 1st time I want n to increase to 1, and then stop. When I run it the second time I want n to increase to 2, etc.
I think you can as per this link: [stackoverflow.com/questions/44012748/…
-1

When you rerun a program, all data stored in memory is reset. You need to save the variable somewhere outside of the program, on disk.

for an example see How to increment variable every time script is run in Python?

ps. Nowadays you can simply do += with a bool:

a = 1
b = True
a += b  # a will be 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.