简体   繁体   English

Python:如何在每次运行 function 时增加代码计数

[英]Python: How to increase count for a code every time a function is run

I am trying to add a function to my project where initial it starts with zero and every time this function is run it increments by 1. The issue here is that I don't want it to reset to zero except if the database of the project is reset.我正在尝试将 function 添加到我的项目中,其中初始值从零开始,每次运行此 function 时它都会递增 1。这里的问题是我不希望它重置为零,除非项目的数据库被重置。

def serial_code():
    count = 0
    count += 1
    return count

print(serial_code())

You have 1 option and that option is to use file.您有 1 个选项,该选项是使用文件。

Create a new .txt file in your directory, let's call it count.txt and then put 0 into that file.在您的目录中创建一个新的.txt文件,我们将其命名为count.txt ,然后将0放入该文件中。

def serial_code():
    called = True

    if called:
        count_file = open("count.txt", "r") # open file in read mode
        count = count_file.read() # read data 
        count_file.close() # close file

        count_file = open("count.txt", "w") # open file again but in write mode
        count = int(count) + 1 # increase the count value by add 1
        count_file.write(str(count)) # write count to file
        count_file.close() # close file

    return count
  • First we open file in read mode to read data from the file which is 0 then assign 0 into count variable and close the file.首先我们以读取模式打开文件以从文件中读取数据,该文件为0 ,然后将 0 赋值给count变量并关闭文件。

  • Second, open file back again but in write mode this time (we do this because write mode will rewrite the whole content), then increase 1, write to file, and close file.其次,再次打开文件,但这次是写模式(我们这样做是因为模式将重写整个内容),然后增加 1,写入文件,然后关闭文件。

Now whenever you call the function the value inside file will increase by 1. 0 -> 1 -> 2 -> 3.....现在,无论何时调用 function,文件中的值都会增加 1。0 -> 1 -> 2 -> 3.....

You can do this using the global -keyword for the variable that should persist beyond the scope of the function. After being initialised once, the variable i in the following code is increased with every call to f :您可以使用global -关键字为应该在 function 的 scope 之后持续存在的变量执行此操作。初始化一次后,每次调用f时,以下代码中的变量i都会增加:

i = 0
def f():
    global i
    i += 1
    
for _ in range(100):
    f()
    print(i)

See your code that u declare the variable in the function itself that's why it always reset declare it outside.查看您在 function 本身中声明变量的代码,这就是为什么它总是在外部重置声明的原因。 And make the variable global并使变量全局

count = 0
def serial_code():
    global count
    count += 1
    return count
print(serial_code())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python每天特定时间运行一个function? - How to run a function every day at a specific time in python? 如何每天在特定时间运行python函数 - How to run a python function every day at specific time Python代码片段需要很长时间才能运行..如何提高效率? - Python code snippet is taking very long time to run.. how to increase efficiency? 如何安排 Python 代码每天运行多次,仅使用 python 代码 - How to schedule Python Code Run Multiple Time Every Day, With using only python code 每次运行函数时如何增加函数变量 - How to increase a function variable each time the function is run python代码有问题,该问题应在每次出现100次时计数 - Having problems with python code that is meant to count every time 100 appears 每次在Python中运行代码时,如何以相同的顺序随机化pandas列? - How do I randomize a pandas column in the same order every time I run the code in Python? Python每天在定义的时间运行一段代码 - Python to run a piece of code at a defined time every day 如何每 1 秒并行运行 python function - How to run python function every 1 sec in parallel 如何在一段时间内在Python上的每个特定时间运行函数? - How can I run a function every specific time within a while cycle on python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM