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
Osuynonma
  • 489
  • 1
  • 7
  • 13

3 Answers3

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.

Ubdus Samad
  • 1,218
  • 1
  • 15
  • 27
  • 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. – Osuynonma Mar 02 '18 at 04:50
  • 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. – Ubdus Samad Mar 02 '18 at 06:16
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.

Ali
  • 43
  • 7
  • 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? – Osuynonma Mar 02 '18 at 04:58
  • Do you mean if you run the program it will increase the n? I did not get what you mean. – Ali Mar 02 '18 at 05:04
  • 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. – Osuynonma Mar 02 '18 at 05:07
  • I think you can as per this link: [https://stackoverflow.com/questions/44012748/how-to-increment-variable-every-time-script-is-run-in-python] – Ali Mar 02 '18 at 05:20
-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
Peter F
  • 420
  • 4
  • 12