Loop Indentation Issue


#1

My code is not performing as expected with some nested loops, possibly an indentation issue. Appreciate anyone's help.

Here is the code:

#Block 1

for generation in range(1,3):    

    #Block 2

    for cycle in range(1,3):

        #Block 3

        for n in org:
            n.perform()

    #Continue Block 2

    rtalive=0
    nrtalive=0

    for k in org:
        if k.state=='alive':
            if k.type==0:
                nrtalive+=1
            else:
                rtalive+=1

Python is actually treating the #Continue Block 2 as Block 3, dont know why.
I need to make sure that the code in the #Continue Block 2 only loops following the 'for generation' loop and not the 'for cycle' loop, as it is doing now.

Rgrds
Elmo


#2

What makes you say that?


#3

On how to format your code to make it forum readable:

Could you describe a bit further what you mean by this? What you see and what you want to see?


#4

Thanks ionatan. Python is looping #Continue Block 2 code with two loops: 'for generation' and 'for cycle', where it should only follow 'for generation' loop.


#5

I don't think it is. I think you've made some incorrect assumption somewhere.
Or maybe you didn't post the exact code you are using?

What you posted will not run on its own, some things are not defined (like org), so we're not able to test it. Make sure that your examples are enough to reproduce what you describe! https://stackoverflow.com/help/mcve

Mixing tabs and spaces could have the effect that you are describing, but I can't tell from your post if that's the case. Your post contains no tab characters.
Python2 considers a tab character to be "worth" 8 spaces, but editors may display them with a width of 4 characters which would lead to your view of the code being misrepresenting of the code.
Python3 refuses to run the code instead, which is probably for the best as it's a likely indication of bugs.


#6

Thx Ionatan,

Here is the full code:

import random
import numpy

class IterRegistry(type):
     def __iter__(cls):
        return iter(cls._registry)
    
class organization:
    __metaclass__ = IterRegistry
    _registry = []

    def __init__(self, type):
        self.type = type
        self.failures=0
        self.resource_A_requirement = int(random.random() * 10)
        self.resource_B_requirement = 10- self.resource_A_requirement
        self.state='alive'
        self._registry.append(self)
       
    
    def perform(self):
        max_resources=10
        resource_A_availability=0.99
        resource_B_availability=0.99
        resource_A_output=0
        resource_B_output=0
        
        for resource_unit in range(1, max_resources):
            if random.random()< resource_A_availability:
                resource_A_output+=1
            if random.random()< resource_B_availability:
                resource_B_output+=1

        if self.type==0:
            self.resource_A_requirement = int(random.random() * 10)
            self.resource_B_requirement = 10- self.resource_A_requirement

        if self.resource_A_requirement >= resource_A_output or self.resource_B_requirement >= resource_B_output:
            self.failures+=1
            if self.failures==5:
                self.state='dead'

        else:
            self.failures=0
            
f = open('simoutput.txt', 'w')
line= 'Generation,Cycle,Non-Routine, Routine' + '\n'
f.write(line)

org=[]                
for n in range(1,50):
    if n<26:
        org.append(organization(0))
    else:
        org.append(organization(1))
    org[n-1].perform()

#Block 1
for generation in range(1,3):    

    #Block 2

    for cycle in range(1,3):

        #Block 3

        for n in org:
            n.perform()

    #Continue Block 2

    rtalive=0
    nrtalive=0

    for k in org:
        if k.state=='alive':
            if k.type==0:
                nrtalive+=1
            else:
                rtalive+=1


    line= str(generation)+',' + str(cycle) +',' + str(nrtalive) + ',' + str(rtalive)+ '\n'
    f.write(line)

    #Create new generation of organizations    
    aliveorgs=[]
    for i in org:
        if i.state=='alive':
                aliveorgs.append(i.type)


    for n in range(1,50):
        itype= int(random.random()*len(aliveorgs))
        tp=org[itype].type
        org.append(organization(tp))


f.close()

#7

Sorry new at the forum and this type of request


#8

That's fine, asking questions and solving problems is nearly the same thing anyhow. Get better at one, get better at the other.

Adding prints at the start/continuation of the blocks and then running your code will output:

begin block 2
block 3
block 3
continue block 2
begin block 2
block 3
block 3
continue block 2

block 3 runs twice in a row without continue block 2 running. So whatever is making you think that block 2 is inside block 3, is a bad assumption.. Unless I've misunderstood what you are saying