Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
auto increment
#1
hi
num=1000
def inc():
    increment = num + 1
     return inc


print(inc())
in inc function the number 1000 is incremented by one and it is giving 1001 .when function is called again it is giving the same value 1001.what should be changed so that on every call to the function the value should increment by one and values like 1001,1002,1003............
Reply
#2
Hello, next time when posting code, please use Python code tags (you can find help here).
increment = num + 1
This line only changes variable increment, instead you want to change num.
return inc
With this code, function returns itself, and not a value you want.
Reply
#3
How about...

num = 1000

def inc():
    global num
    num += 1
    return num

for x in range(10):
    print(inc())
Reply
#4
You don't need a global variable
$ python3
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def generate_incrementer(start):
...     num = start
...     def incrementer():
...         nonlocal num
...         num += 1
...         return num
...     return incrementer
... 
>>> incrementer = generate_incrementer(999)
>>> print(incrementer())
1000
>>> print(incrementer())
1001
In fact, I wouldn't make incrementer global unless you really need it to be.

And if you don't actually need a function to do it, you can use a built-in
>>> from itertools import count
>>> counter = count(1000)
>>> next(counter)
1000
>>> next(counter)
1001
If you can tell us your larger goal we might be able to provide a better way, still.
Reply
#5
Or a class! (and my axe!)
>>> class counter:
...   def __init__(self, initial=0):
...     self.value = initial
...   def inc(self, increment_by=1):
...     self.value += increment_by
...     return self.value
...   def __call__(self):
...     return self.inc()
...
>>> inc = counter(1000)
>>> for _ in range(10):
...   print(inc())
...
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
Reply
#6
It's probably all the functional programming I've been doing that has me thinking in terms of closures :)
Reply
#7
In all seriousness though, a closure is just a class with less steps lol
Reply
#8
I would name the class Counter.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help to increment a third list hermine 7 1,327 Nov-29-2022, 04:19 PM
Last Post: perfringo
  mysql id auto increment not working tantony 10 2,410 Oct-18-2022, 11:43 PM
Last Post: Pedroski55
  Character Increment AnokhiRaaz 1 2,498 Apr-22-2021, 04:29 AM
Last Post: buran
  Increment text files output and limit contains Kaminsky 1 3,188 Jan-30-2021, 06:58 PM
Last Post: bowlofred
  Increment formula Kristenl2784 4 2,873 Jul-20-2020, 10:14 PM
Last Post: Kristenl2784
  [openpyxl] Increment cells being pasted into Template Kristenl2784 4 3,564 Jul-16-2020, 10:00 PM
Last Post: Kristenl2784
  How can I increment a List item with in a "for in" msteffes 4 3,549 Aug-14-2019, 08:55 AM
Last Post: DeaD_EyE
  How define iteration interval increment SriMekala 5 4,326 Jun-01-2019, 01:06 PM
Last Post: ichabod801
  SQlite3 quickly increment INT value? jmair 1 2,442 Mar-04-2019, 08:03 PM
Last Post: stranac
  increment variable in while loop Naito 3 2,965 Jan-20-2019, 12:30 PM
Last Post: Naito

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020