Python Forum
variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0
#2
def DoIt(h5):
    global reset,xP,yP, vcnt, ocnt, mcnt
###
            for h4 in hVP:
                count(h4)
###
            vs.AlrtDialog(ocnt) #Displays current count but for somereason is adding the previous count to the current count
            vcnt = ocnt = mcnt = 0 #reset to variable to 0
            vs.AlrtDialog(ocnt) #displays that variable has been reset to 0
count() is where you're setting them. You call count() before you reset them. That's why they have values before you reset them.

And wackiness like that is exactly why you should avoid using globals at all cost. They're hard to debug, and extremely lazy. What's wrong with just returning whatever values you want to use from a function?

import vs
#reset = xP = yP = vcnt = ocnt = mcnt = 0
 
def count(h4, ocnt, vcnt, mcnt):
 #   global vcnt, ocnt, mcnt
    ocnt += 1
    if vs.GetTypeN(h4) == 5:
        vcnt += vs.GetVertNum(h4)
    if vs.GetTypeN(h4) == 21:
        vcnt += vs.GetVertNum(h4)
    if vs.GetTypeN(h4) == 40:
        mcnt += 1           
    return [ocnt, vcnt, mcnt]
 
def DoIt(h5):
    #global reset,xP,yP, vcnt, ocnt, mcnt
    ocnt, vcnt, mcnt = (0, 0, 0)
    if h5 != []:
        if vs.GetTypeN(h5) == 16:
            GetHandles(h5)
            for h4 in hVP:
                ocnt, vcnt, mcnt = count(h4, ocnt, vcnt, mcnt)
            vs.SetRecord(h5,'Obj Count')
            vs.SetRField(h5,'Obj Count','Obj Count',vs.Num2Str(0,ocnt))
            vs.AlrtDialog(ocnt) #Displays current count but for somereason is adding the previous count to the current count
            vcnt = ocnt = mcnt = 0 #reset to variable to 0
            vs.AlrtDialog(ocnt) #displays that variable has been reset to 0
Reply


Messages In This Thread
RE: variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 - by nilamo - Feb-12-2021, 06:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  File path by adding various variables Mishal0488 2 1,023 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,629 Jan-24-2023, 08:22 AM
Last Post: perfringo
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,469 Jul-27-2022, 08:50 PM
Last Post: rob101
  Create array of values from 2 variables paulo79 1 1,080 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  Loop through values in dictrionary and find the same as in previous row Paqqno 5 1,892 Mar-27-2022, 07:58 PM
Last Post: deanhystad
  resetting an iterator to full Skaperen 7 6,943 Feb-20-2022, 11:11 PM
Last Post: Skaperen
  Calculate next rows based on previous values of array divon 0 1,758 Nov-23-2021, 04:44 AM
Last Post: divon
  Giving all possible values to four different variables quest_ 7 2,940 Jan-18-2021, 05:18 AM
Last Post: deanhystad
  Adding keys and values to a dictionary giladal 3 2,466 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  Variables being overridden to initial values. p2bc 6 2,617 Oct-10-2020, 09:03 PM
Last Post: p2bc

Forum Jump:

User Panel Messages

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