Home > Mobile >  Increment the value of a variable once every loop in python
Increment the value of a variable once every loop in python

Time:10-24

I have variable Its value is 4 for example, I want it at the beginning of each loop to increase by 1 each time

CodePudding user response:

You have just to make an increment at the beginning of your for loop. For example:

var = 4

for i in range(your_range):
      var  = 1
      # do your stuff
 

CodePudding user response:

You've mentioned "at the beginning of each loop", so assuming you iterate something (a in this case):

a = ['a','b','c']
value = 4

for char in a:
    value  =1
    print(value)

#output
5
6
7
  • Related