Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:01:24+00:00 2026-06-12T00:01:24+00:00

I am new to python. My code runs into infinite loop, keeps adding &

  • 0

I am new to python. My code runs into infinite loop, keeps adding & printing numberCycles. My logic in C++ works fine.
Would you please help to identify why?

  • The first line of input is the number of simulations.
  • The next line is the number of minutes for a single reproductive cycle.
  • The next line is the number of rows (x), followed by a single space, and followed by number of columns (y).
  • The next group of y lines will have x number of characters, with a single period (.) representing a blank space and a single capitol B representing a starting Bunny tries to reproduce to up, right, down, left direction. If there is a existing bunny, then it goes to sleep.

Input.txt

2     # 2 simulations
5     # 5 minutes/cycle
3 3   # 3*3 map
...
.B.
...
1
4 4
B.BB
..B.
...
B.B

Spot.py

#!/usr/bin/env python

class Spot(object):
    isBunny = bool()
    nextCycle = 0
    UP = 0
    RIGHT = 1
    DOWN = 2
    LEFT = 3
    SLEEP = 4

    def __init__(self, newIsBunny):
        self.isBunny = newIsBunny
        self.nextCycle = self.UP

    def setNextCycle(self):
        if (self.nextCycle != self.SLEEP):
            self.nextCycle += 1

    def getNextCycle(self):
        return self.nextCycle

    def getIsBunny(self):
        return self.isBunny

    def makeBunny(self):
        if not self.isBunny:
            self.nextCycle = self.UP
        self.isBunny = True

Bunny.py

#!/usr/bin/env python
from Spot import Spot
import os

class Bunny(object):    
    @classmethod
    def main(cls, args):
        with open(os.path.expanduser('~/Desktop/input.txt')) as f: 
            numSims = int(f.readline()) 
            myMap = []  
            print numSims
            minPerCycle= int(f.readline()) 
            print minPerCycle
            for k in xrange(numSims): 
                xyLine= f.readline()
                row = int(xyLine.split()[0]) 
                col = int(xyLine.split()[1])  
                print row,col
                for i in range(0,row): 
                    myLine = f.readline() 
                    myMap.append([]) 
                    for j in range(0,col): 
                        myMap[i].append(Spot(myLine[j] == 'B')) 

                numCycles = 1

                if cls.isFilled(row,col,myMap):
                    numCycles = 0

                while not cls.isFilled(row,col,myMap):
                    numCycles += 1
                    print numCycles
                    for m in range(0,row): 
                        for n in range(0,col): 
                            if myMap[m][n].getIsBunny():
                                if myMap[m][n].getNextCycle() == Spot.UP:
                                    if m>0:
                                        myMap[m-1][n].makeBunny()
                                    break
                                elif myMap[m][n].getNextCycle() == Spot.RIGHT:
                                    if n<col-1:
                                        myMap[m][n+1].makeBunny()
                                    break
                                elif myMap[m][n].getNextCycle() == Spot.DOWN:
                                    if m<row-1:
                                        myMap[m+ 1][n].makeBunny()
                                    break
                                elif myMap[m][n].getNextCycle() == Spot.SLEEP:
                                    if n>0:
                                        myMap[m][n- 1].makeBunny()
                                    break
                            myMap[m][n].setNextCycle() 
                time = numCycles * minPerCycle
                print "It took " , time , " minutes for the bunnies to take over the world!\n"
                del myMap[:]
            f.close()

    @classmethod
    def isFilled(cls,row,col,myMap):
        for a in range(0,row): 
            for b in range(0,col): 
                if not myMap[a][b].getIsBunny():
                    return False
        return True


if __name__ == '__main__':
    import sys
    Bunny.main(sys.argv)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:01:25+00:00Added an answer on June 12, 2026 at 12:01 am

    It never goes to the left. Also, you set a break for the inner loop, so, myMap[m][n].setNextCycle() is never called. I just saw your C++ code and you translated it to Python with errors (using Spot.SLEEP instead of Spot.LEFT).

    The break statement makes sense in C++ because you want to break the switch. Here, you are using if..else.

    It should be something like:

    while not cls.isFilled(row, col, myMap):
        numCycles += 1
        print numCycles
        for m in range(0,row): 
            for n in range(0,col): 
                if myMap[m][n].getIsBunny():
                    if myMap[m][n].getNextCycle() == Spot.UP:
                        if m>0:
                            myMap[m-1][n].makeBunny()
                    elif myMap[m][n].getNextCycle() == Spot.RIGHT:
                        if n<col-1:
                            myMap[m][n+1].makeBunny()
                    elif myMap[m][n].getNextCycle() == Spot.DOWN:
                        if m<row-1:
                            myMap[m+ 1][n].makeBunny()
                    elif myMap[m][n].getNextCycle() == Spot.LEFT:
                        if n>0:
                            myMap[m][n-1].makeBunny()
    
                myMap[m][n].setNextCycle()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new at python. Here is my wiered python code Which works fine
I'm trying to create a new CodeType, the following code runs just fine in
I have a piece of Python code that is supposed to open a new
New to programming. I wrote a Python code (with help from stackoverflow) to read
I am new to Python. The following code is causing an error when it
I'm fairly new to Python, so I'm trying my hand at some simple code.
I am new to coding python. I am hoping to modify this code to
I'm just learning about python. I'm fairly new. I have the following code that
I am new to TCP/IP connection using python I have this simple code: import
I'm new with Python and I know that piece of code is very simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.