Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lifetime cycle counter
#1
I made the generic code to do the job i want, but i want to also keep track of the total number of cycles the machine does. to keep this data when the computer turns off i need to save the cycle count value to a .txt document, then read it every time a cycle starts, add 1 to it when a cycle completes, delete the old value, and save the new one. repeat this every time a cycle happens.

i use a for loop to govern the number of cycles to be run based off of external input from another program, and it works. in the for loop i have a print(1 cycle) so i can see that it runs the correct number of cycles, which it does. However, when i callout my cycle counter function within this for loop it only runs the function once while still running everything else the correct number of times.

this means that in my .txt file the number only increases by 1 every time instead of increasing by how many cycles it did.

ill put in the code, but it wont make much sense to you without the other program too. basically create the .txt file "Cycle.txt" and put in any number and a "lifecounter.txt" file and put in 0 then the important part will work.

def countmath():
    counttxt = open('lifecounter.txt', 'r+')
    for line in counttxt:
        countlist.append(line)
    D = countlist[0]
    counter = int(D)
    counter += 1  # adds 1 to the counted value
    counttxt.truncate()  # deletes previous file data
    counttxt.close()

    E = str(counter)
    counttxt1 = open('lifecounter.txt', 'r+')
    counttxt1.write(E)
    counttxt1.close()


store=[]             #creates empty list
cycles= open('Cycle.txt','rt')          #opens "cycle.txt" file under the name "cycles" with the intention to read text
for line in cycles:      #reads each line of .txt file seperately 1 after the other
    store.append(line)    #adds the currently read line to the list "store"

A= store[1]  #Assigns the variable "A" the value of line 1 in the newly formed list
B= store[2]
C= store[3]

cyclesD=int(A)    #convert list items to integer values and assign them to new variables
rateD=int(B)
powerD=int(C)

countlist = []

for lifecount in range (cyclesD):     #loops the process the number of times as in the parenthasis.
    print("1 Cycle")
    countmath()



if powerD==(1):
    print("shutting down")
    exit()
if powerD==(0):
    print("Job Complete")
    exit()
i know this is a cluster, but im just learning
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to fix SyntaxError in cycle try alexfrol86 14 3,061 Mar-27-2022, 07:53 AM
Last Post: stevendaprano
  Trying to cycle through a list of charcters pooky2483 12 4,316 Sep-28-2020, 06:55 AM
Last Post: pooky2483
  <while> cycle is not interrupted when using the <random>module ShityCoder 3 2,106 Sep-04-2020, 04:05 PM
Last Post: ShityCoder
  Cycle of numpy variables Zero01 0 1,537 Jul-31-2020, 11:58 AM
Last Post: Zero01
  stop cycle while windows11 1 1,972 May-16-2020, 03:17 PM
Last Post: deanhystad
  Occurrences using FOR and IF cycle P86 2 2,477 Jul-29-2019, 04:37 PM
Last Post: ThomasL
  pool map cycle skorost5 5 3,757 Apr-07-2019, 09:21 AM
Last Post: skorost5

Forum Jump:

User Panel Messages

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