Python gets into an infinite loop that does nothing











up vote
0
down vote

favorite












I don't know why it's happening but basically after python should end a while, it does nothing (but the program doesn't finish) and I can see the program is using processor as if it was an infinite loop.



I have no clue why it happens, it wasn't happening before and I think I haven't changed anything on the function that's causing this issue.



Code:



import random


class Gato:

movimiento = 0
posicion = [0, 0]

def spawn(self):
self.posicion = [random.randint(0, 4), random.randint(0, 4)]

def moverse(self, direccion):
movido = False

while not movido:
if direccion == "sur":
if self.posicion[1] == 4:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (norte, este, oeste)")
else:
self.posicion[1] += 1
self.movimiento += 1
movido = True
elif direccion == "norte":
if self.posicion[1] == 0:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (sur, este, oeste)")
else:
self.posicion[1] -= 1
self.movimiento += 1
movido = True
elif direccion == "este":
if self.posicion[0] == 4:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (norte, sur, oeste)")
else:
self.posicion[0] += 1
self.movimiento += 1
movido = True
elif direccion == "oeste":
if self.posicion[0] == 0:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (norte, sur, este)")
else:
self.posicion[0] -= 1
self.movimiento += 1
movido = True
else:
print("Esa no es una direccion válida")
direccion = input("Vuelve a moverte en una dirección (norte, sur, este)")

def detectarcolisionraton(self, raton):
if self.posicion == raton.getposicion():
return True
else:
return False

def detectarcolisionqueso(self, queso):
if self.posicion == queso.getposicion():
return True
else:
return False

def getmovimientos(self):
return self.movimiento

def checklimites(self, dir):
if self.posicion[1] == 4 & dir == 0:
print("No puedes salir de los limites del mapa")
elif self.posicion[1] == 0 & dir == 1:
print("No puedes salir de los limites del mapa")
elif self.posicion[0] == 0 & dir == 2:
print("No puedes salir de los limites del mapa")
elif self.posicion[0] == 4 & dir == 3:
print("No puedes salir de los limites del mapa")


class Raton:

posicion = [0, 0]

def spawn(self):
self.posicion = [random.randint(0, 4), random.randint(0, 4)]

def getposicion(self):
return self.posicion

def moverse(self, gato):
xory = random.randint(0, 1)
rand = random.randint(0, 1)
movido = False

while not movido:
if gato.getmovimientos() % 3 == 0:
if xory == 0:
if rand == 0:
if self.posicion[0] == 4:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[0] += 1
movido = True
else:
if self.posicion[0] == 0:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[0] -= 1
movido = True
else:
if rand == 0:
if self.posicion[1] == 4:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[1] += 1
movido = True
else:
if self.posicion[1] == 0:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[1] -= 1
movido = True

def pista(self, gato):
if self.posicion[0] == gato.posicion[0] and self.posicion[1] > gato.posicion[1]:
print("El raton está al Sur")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] > gato.posicion[1]:
print("El raton está al Sureste")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] == gato.posicion[1]:
print("El raton está al Este")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Noreste")
elif self.posicion[0] == gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Norte")
elif self.posicion[0] < gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Noroeste")
elif self.posicion[0] < gato.posicion[0] and self.posicion[1] == gato.posicion[1]:
print("El raton está al Oeste")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Suroeste")


class Queso:

posicion = [0, 0]

def spawn(self):
self.posicion = [random.randint(0, 4), random.randint(0, 4)]

def getposicion(self):
return self.posicion


def imprimirmapa():
for y in range(0, 5):
for x in range(0, 5):
print("[", end="", flush=True)
if gato.posicion[0] == x and gato.posicion[1] == y:
print("O", end="", flush=True)
else:
print("X", end="", flush=True)

if x == 4:
print("]")
else:
print("]", end="", flush=True)


gato = Gato()
raton = Raton()
queso = Queso()

gato.spawn()
raton.spawn()
queso.spawn()

while gato.posicion[0] == raton.posicion[0] and gato.posicion[1] == raton.posicion[1]:
print("Spawn Failed!")
raton.spawn()


ganado = False

while not ganado:
raton.pista(gato)
imprimirmapa()
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
if gato.detectarcolisionraton(raton):
print("Has ganado")
ganado = True
elif gato.detectarcolisionqueso(queso):
queso.posicion = [-1, -1]
print("Encontraste el QUESO")
raton.pista(gato)
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
raton.pista(gato)
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
raton.pista(gato)
queso.spawn()
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
raton.moverse(gato)









share|improve this question






















  • Check all your while loops. Is it possible that within one of your while loops, you never change the while condition?
    – Joel
    Nov 7 at 22:51










  • I would strongly recommend trying to debug this yourself before coming here. Did you use an IDE with a debugger, and step through each line to see where the infinite loop was happening? If not, I'd again strongly recommend learning how to do that, since it will be a necessary tool for every programmer at some point.
    – Random Davis
    Nov 7 at 22:52










  • Just a guess, but it seems to get stuck in this loop: while not movido: you set movido = False, and while not movido evaluates to while True and never set movido = False so it's continually checking while True. Possibly should the loop be while movido: ... ?
    – davedwards
    Nov 7 at 23:00












  • @RandomDavis Already tried to debug it though I'm not very familiar with pycharm (which is the ide I'm using), I'm a Java guy haha
    – Oscar Arranz
    Nov 7 at 23:06










  • @davedwards I tried to change the condition to movido == False but it's still doing the same thing, and I do change it to true after I type in the north/south/east/west thing. It was working before, I am really confused
    – Oscar Arranz
    Nov 7 at 23:08















up vote
0
down vote

favorite












I don't know why it's happening but basically after python should end a while, it does nothing (but the program doesn't finish) and I can see the program is using processor as if it was an infinite loop.



I have no clue why it happens, it wasn't happening before and I think I haven't changed anything on the function that's causing this issue.



Code:



import random


class Gato:

movimiento = 0
posicion = [0, 0]

def spawn(self):
self.posicion = [random.randint(0, 4), random.randint(0, 4)]

def moverse(self, direccion):
movido = False

while not movido:
if direccion == "sur":
if self.posicion[1] == 4:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (norte, este, oeste)")
else:
self.posicion[1] += 1
self.movimiento += 1
movido = True
elif direccion == "norte":
if self.posicion[1] == 0:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (sur, este, oeste)")
else:
self.posicion[1] -= 1
self.movimiento += 1
movido = True
elif direccion == "este":
if self.posicion[0] == 4:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (norte, sur, oeste)")
else:
self.posicion[0] += 1
self.movimiento += 1
movido = True
elif direccion == "oeste":
if self.posicion[0] == 0:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (norte, sur, este)")
else:
self.posicion[0] -= 1
self.movimiento += 1
movido = True
else:
print("Esa no es una direccion válida")
direccion = input("Vuelve a moverte en una dirección (norte, sur, este)")

def detectarcolisionraton(self, raton):
if self.posicion == raton.getposicion():
return True
else:
return False

def detectarcolisionqueso(self, queso):
if self.posicion == queso.getposicion():
return True
else:
return False

def getmovimientos(self):
return self.movimiento

def checklimites(self, dir):
if self.posicion[1] == 4 & dir == 0:
print("No puedes salir de los limites del mapa")
elif self.posicion[1] == 0 & dir == 1:
print("No puedes salir de los limites del mapa")
elif self.posicion[0] == 0 & dir == 2:
print("No puedes salir de los limites del mapa")
elif self.posicion[0] == 4 & dir == 3:
print("No puedes salir de los limites del mapa")


class Raton:

posicion = [0, 0]

def spawn(self):
self.posicion = [random.randint(0, 4), random.randint(0, 4)]

def getposicion(self):
return self.posicion

def moverse(self, gato):
xory = random.randint(0, 1)
rand = random.randint(0, 1)
movido = False

while not movido:
if gato.getmovimientos() % 3 == 0:
if xory == 0:
if rand == 0:
if self.posicion[0] == 4:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[0] += 1
movido = True
else:
if self.posicion[0] == 0:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[0] -= 1
movido = True
else:
if rand == 0:
if self.posicion[1] == 4:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[1] += 1
movido = True
else:
if self.posicion[1] == 0:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[1] -= 1
movido = True

def pista(self, gato):
if self.posicion[0] == gato.posicion[0] and self.posicion[1] > gato.posicion[1]:
print("El raton está al Sur")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] > gato.posicion[1]:
print("El raton está al Sureste")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] == gato.posicion[1]:
print("El raton está al Este")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Noreste")
elif self.posicion[0] == gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Norte")
elif self.posicion[0] < gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Noroeste")
elif self.posicion[0] < gato.posicion[0] and self.posicion[1] == gato.posicion[1]:
print("El raton está al Oeste")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Suroeste")


class Queso:

posicion = [0, 0]

def spawn(self):
self.posicion = [random.randint(0, 4), random.randint(0, 4)]

def getposicion(self):
return self.posicion


def imprimirmapa():
for y in range(0, 5):
for x in range(0, 5):
print("[", end="", flush=True)
if gato.posicion[0] == x and gato.posicion[1] == y:
print("O", end="", flush=True)
else:
print("X", end="", flush=True)

if x == 4:
print("]")
else:
print("]", end="", flush=True)


gato = Gato()
raton = Raton()
queso = Queso()

gato.spawn()
raton.spawn()
queso.spawn()

while gato.posicion[0] == raton.posicion[0] and gato.posicion[1] == raton.posicion[1]:
print("Spawn Failed!")
raton.spawn()


ganado = False

while not ganado:
raton.pista(gato)
imprimirmapa()
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
if gato.detectarcolisionraton(raton):
print("Has ganado")
ganado = True
elif gato.detectarcolisionqueso(queso):
queso.posicion = [-1, -1]
print("Encontraste el QUESO")
raton.pista(gato)
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
raton.pista(gato)
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
raton.pista(gato)
queso.spawn()
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
raton.moverse(gato)









share|improve this question






















  • Check all your while loops. Is it possible that within one of your while loops, you never change the while condition?
    – Joel
    Nov 7 at 22:51










  • I would strongly recommend trying to debug this yourself before coming here. Did you use an IDE with a debugger, and step through each line to see where the infinite loop was happening? If not, I'd again strongly recommend learning how to do that, since it will be a necessary tool for every programmer at some point.
    – Random Davis
    Nov 7 at 22:52










  • Just a guess, but it seems to get stuck in this loop: while not movido: you set movido = False, and while not movido evaluates to while True and never set movido = False so it's continually checking while True. Possibly should the loop be while movido: ... ?
    – davedwards
    Nov 7 at 23:00












  • @RandomDavis Already tried to debug it though I'm not very familiar with pycharm (which is the ide I'm using), I'm a Java guy haha
    – Oscar Arranz
    Nov 7 at 23:06










  • @davedwards I tried to change the condition to movido == False but it's still doing the same thing, and I do change it to true after I type in the north/south/east/west thing. It was working before, I am really confused
    – Oscar Arranz
    Nov 7 at 23:08













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I don't know why it's happening but basically after python should end a while, it does nothing (but the program doesn't finish) and I can see the program is using processor as if it was an infinite loop.



I have no clue why it happens, it wasn't happening before and I think I haven't changed anything on the function that's causing this issue.



Code:



import random


class Gato:

movimiento = 0
posicion = [0, 0]

def spawn(self):
self.posicion = [random.randint(0, 4), random.randint(0, 4)]

def moverse(self, direccion):
movido = False

while not movido:
if direccion == "sur":
if self.posicion[1] == 4:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (norte, este, oeste)")
else:
self.posicion[1] += 1
self.movimiento += 1
movido = True
elif direccion == "norte":
if self.posicion[1] == 0:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (sur, este, oeste)")
else:
self.posicion[1] -= 1
self.movimiento += 1
movido = True
elif direccion == "este":
if self.posicion[0] == 4:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (norte, sur, oeste)")
else:
self.posicion[0] += 1
self.movimiento += 1
movido = True
elif direccion == "oeste":
if self.posicion[0] == 0:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (norte, sur, este)")
else:
self.posicion[0] -= 1
self.movimiento += 1
movido = True
else:
print("Esa no es una direccion válida")
direccion = input("Vuelve a moverte en una dirección (norte, sur, este)")

def detectarcolisionraton(self, raton):
if self.posicion == raton.getposicion():
return True
else:
return False

def detectarcolisionqueso(self, queso):
if self.posicion == queso.getposicion():
return True
else:
return False

def getmovimientos(self):
return self.movimiento

def checklimites(self, dir):
if self.posicion[1] == 4 & dir == 0:
print("No puedes salir de los limites del mapa")
elif self.posicion[1] == 0 & dir == 1:
print("No puedes salir de los limites del mapa")
elif self.posicion[0] == 0 & dir == 2:
print("No puedes salir de los limites del mapa")
elif self.posicion[0] == 4 & dir == 3:
print("No puedes salir de los limites del mapa")


class Raton:

posicion = [0, 0]

def spawn(self):
self.posicion = [random.randint(0, 4), random.randint(0, 4)]

def getposicion(self):
return self.posicion

def moverse(self, gato):
xory = random.randint(0, 1)
rand = random.randint(0, 1)
movido = False

while not movido:
if gato.getmovimientos() % 3 == 0:
if xory == 0:
if rand == 0:
if self.posicion[0] == 4:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[0] += 1
movido = True
else:
if self.posicion[0] == 0:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[0] -= 1
movido = True
else:
if rand == 0:
if self.posicion[1] == 4:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[1] += 1
movido = True
else:
if self.posicion[1] == 0:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[1] -= 1
movido = True

def pista(self, gato):
if self.posicion[0] == gato.posicion[0] and self.posicion[1] > gato.posicion[1]:
print("El raton está al Sur")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] > gato.posicion[1]:
print("El raton está al Sureste")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] == gato.posicion[1]:
print("El raton está al Este")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Noreste")
elif self.posicion[0] == gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Norte")
elif self.posicion[0] < gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Noroeste")
elif self.posicion[0] < gato.posicion[0] and self.posicion[1] == gato.posicion[1]:
print("El raton está al Oeste")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Suroeste")


class Queso:

posicion = [0, 0]

def spawn(self):
self.posicion = [random.randint(0, 4), random.randint(0, 4)]

def getposicion(self):
return self.posicion


def imprimirmapa():
for y in range(0, 5):
for x in range(0, 5):
print("[", end="", flush=True)
if gato.posicion[0] == x and gato.posicion[1] == y:
print("O", end="", flush=True)
else:
print("X", end="", flush=True)

if x == 4:
print("]")
else:
print("]", end="", flush=True)


gato = Gato()
raton = Raton()
queso = Queso()

gato.spawn()
raton.spawn()
queso.spawn()

while gato.posicion[0] == raton.posicion[0] and gato.posicion[1] == raton.posicion[1]:
print("Spawn Failed!")
raton.spawn()


ganado = False

while not ganado:
raton.pista(gato)
imprimirmapa()
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
if gato.detectarcolisionraton(raton):
print("Has ganado")
ganado = True
elif gato.detectarcolisionqueso(queso):
queso.posicion = [-1, -1]
print("Encontraste el QUESO")
raton.pista(gato)
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
raton.pista(gato)
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
raton.pista(gato)
queso.spawn()
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
raton.moverse(gato)









share|improve this question













I don't know why it's happening but basically after python should end a while, it does nothing (but the program doesn't finish) and I can see the program is using processor as if it was an infinite loop.



I have no clue why it happens, it wasn't happening before and I think I haven't changed anything on the function that's causing this issue.



Code:



import random


class Gato:

movimiento = 0
posicion = [0, 0]

def spawn(self):
self.posicion = [random.randint(0, 4), random.randint(0, 4)]

def moverse(self, direccion):
movido = False

while not movido:
if direccion == "sur":
if self.posicion[1] == 4:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (norte, este, oeste)")
else:
self.posicion[1] += 1
self.movimiento += 1
movido = True
elif direccion == "norte":
if self.posicion[1] == 0:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (sur, este, oeste)")
else:
self.posicion[1] -= 1
self.movimiento += 1
movido = True
elif direccion == "este":
if self.posicion[0] == 4:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (norte, sur, oeste)")
else:
self.posicion[0] += 1
self.movimiento += 1
movido = True
elif direccion == "oeste":
if self.posicion[0] == 0:
print("No puedes salir de los limites del mapa")
direccion = input("Vuelve a moverte en una dirección (norte, sur, este)")
else:
self.posicion[0] -= 1
self.movimiento += 1
movido = True
else:
print("Esa no es una direccion válida")
direccion = input("Vuelve a moverte en una dirección (norte, sur, este)")

def detectarcolisionraton(self, raton):
if self.posicion == raton.getposicion():
return True
else:
return False

def detectarcolisionqueso(self, queso):
if self.posicion == queso.getposicion():
return True
else:
return False

def getmovimientos(self):
return self.movimiento

def checklimites(self, dir):
if self.posicion[1] == 4 & dir == 0:
print("No puedes salir de los limites del mapa")
elif self.posicion[1] == 0 & dir == 1:
print("No puedes salir de los limites del mapa")
elif self.posicion[0] == 0 & dir == 2:
print("No puedes salir de los limites del mapa")
elif self.posicion[0] == 4 & dir == 3:
print("No puedes salir de los limites del mapa")


class Raton:

posicion = [0, 0]

def spawn(self):
self.posicion = [random.randint(0, 4), random.randint(0, 4)]

def getposicion(self):
return self.posicion

def moverse(self, gato):
xory = random.randint(0, 1)
rand = random.randint(0, 1)
movido = False

while not movido:
if gato.getmovimientos() % 3 == 0:
if xory == 0:
if rand == 0:
if self.posicion[0] == 4:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[0] += 1
movido = True
else:
if self.posicion[0] == 0:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[0] -= 1
movido = True
else:
if rand == 0:
if self.posicion[1] == 4:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[1] += 1
movido = True
else:
if self.posicion[1] == 0:
xory = random.randint(0, 1)
rand = random.randint(0, 1)
else:
self.posicion[1] -= 1
movido = True

def pista(self, gato):
if self.posicion[0] == gato.posicion[0] and self.posicion[1] > gato.posicion[1]:
print("El raton está al Sur")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] > gato.posicion[1]:
print("El raton está al Sureste")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] == gato.posicion[1]:
print("El raton está al Este")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Noreste")
elif self.posicion[0] == gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Norte")
elif self.posicion[0] < gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Noroeste")
elif self.posicion[0] < gato.posicion[0] and self.posicion[1] == gato.posicion[1]:
print("El raton está al Oeste")
elif self.posicion[0] > gato.posicion[0] and self.posicion[1] < gato.posicion[1]:
print("El raton está al Suroeste")


class Queso:

posicion = [0, 0]

def spawn(self):
self.posicion = [random.randint(0, 4), random.randint(0, 4)]

def getposicion(self):
return self.posicion


def imprimirmapa():
for y in range(0, 5):
for x in range(0, 5):
print("[", end="", flush=True)
if gato.posicion[0] == x and gato.posicion[1] == y:
print("O", end="", flush=True)
else:
print("X", end="", flush=True)

if x == 4:
print("]")
else:
print("]", end="", flush=True)


gato = Gato()
raton = Raton()
queso = Queso()

gato.spawn()
raton.spawn()
queso.spawn()

while gato.posicion[0] == raton.posicion[0] and gato.posicion[1] == raton.posicion[1]:
print("Spawn Failed!")
raton.spawn()


ganado = False

while not ganado:
raton.pista(gato)
imprimirmapa()
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
if gato.detectarcolisionraton(raton):
print("Has ganado")
ganado = True
elif gato.detectarcolisionqueso(queso):
queso.posicion = [-1, -1]
print("Encontraste el QUESO")
raton.pista(gato)
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
raton.pista(gato)
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
raton.pista(gato)
queso.spawn()
gato.moverse(input("Introduce la dirección en que te quieres mover (norte, sur, este, oeste)"))
raton.moverse(gato)






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 22:42









Oscar Arranz

4717




4717












  • Check all your while loops. Is it possible that within one of your while loops, you never change the while condition?
    – Joel
    Nov 7 at 22:51










  • I would strongly recommend trying to debug this yourself before coming here. Did you use an IDE with a debugger, and step through each line to see where the infinite loop was happening? If not, I'd again strongly recommend learning how to do that, since it will be a necessary tool for every programmer at some point.
    – Random Davis
    Nov 7 at 22:52










  • Just a guess, but it seems to get stuck in this loop: while not movido: you set movido = False, and while not movido evaluates to while True and never set movido = False so it's continually checking while True. Possibly should the loop be while movido: ... ?
    – davedwards
    Nov 7 at 23:00












  • @RandomDavis Already tried to debug it though I'm not very familiar with pycharm (which is the ide I'm using), I'm a Java guy haha
    – Oscar Arranz
    Nov 7 at 23:06










  • @davedwards I tried to change the condition to movido == False but it's still doing the same thing, and I do change it to true after I type in the north/south/east/west thing. It was working before, I am really confused
    – Oscar Arranz
    Nov 7 at 23:08


















  • Check all your while loops. Is it possible that within one of your while loops, you never change the while condition?
    – Joel
    Nov 7 at 22:51










  • I would strongly recommend trying to debug this yourself before coming here. Did you use an IDE with a debugger, and step through each line to see where the infinite loop was happening? If not, I'd again strongly recommend learning how to do that, since it will be a necessary tool for every programmer at some point.
    – Random Davis
    Nov 7 at 22:52










  • Just a guess, but it seems to get stuck in this loop: while not movido: you set movido = False, and while not movido evaluates to while True and never set movido = False so it's continually checking while True. Possibly should the loop be while movido: ... ?
    – davedwards
    Nov 7 at 23:00












  • @RandomDavis Already tried to debug it though I'm not very familiar with pycharm (which is the ide I'm using), I'm a Java guy haha
    – Oscar Arranz
    Nov 7 at 23:06










  • @davedwards I tried to change the condition to movido == False but it's still doing the same thing, and I do change it to true after I type in the north/south/east/west thing. It was working before, I am really confused
    – Oscar Arranz
    Nov 7 at 23:08
















Check all your while loops. Is it possible that within one of your while loops, you never change the while condition?
– Joel
Nov 7 at 22:51




Check all your while loops. Is it possible that within one of your while loops, you never change the while condition?
– Joel
Nov 7 at 22:51












I would strongly recommend trying to debug this yourself before coming here. Did you use an IDE with a debugger, and step through each line to see where the infinite loop was happening? If not, I'd again strongly recommend learning how to do that, since it will be a necessary tool for every programmer at some point.
– Random Davis
Nov 7 at 22:52




I would strongly recommend trying to debug this yourself before coming here. Did you use an IDE with a debugger, and step through each line to see where the infinite loop was happening? If not, I'd again strongly recommend learning how to do that, since it will be a necessary tool for every programmer at some point.
– Random Davis
Nov 7 at 22:52












Just a guess, but it seems to get stuck in this loop: while not movido: you set movido = False, and while not movido evaluates to while True and never set movido = False so it's continually checking while True. Possibly should the loop be while movido: ... ?
– davedwards
Nov 7 at 23:00






Just a guess, but it seems to get stuck in this loop: while not movido: you set movido = False, and while not movido evaluates to while True and never set movido = False so it's continually checking while True. Possibly should the loop be while movido: ... ?
– davedwards
Nov 7 at 23:00














@RandomDavis Already tried to debug it though I'm not very familiar with pycharm (which is the ide I'm using), I'm a Java guy haha
– Oscar Arranz
Nov 7 at 23:06




@RandomDavis Already tried to debug it though I'm not very familiar with pycharm (which is the ide I'm using), I'm a Java guy haha
– Oscar Arranz
Nov 7 at 23:06












@davedwards I tried to change the condition to movido == False but it's still doing the same thing, and I do change it to true after I type in the north/south/east/west thing. It was working before, I am really confused
– Oscar Arranz
Nov 7 at 23:08




@davedwards I tried to change the condition to movido == False but it's still doing the same thing, and I do change it to true after I type in the north/south/east/west thing. It was working before, I am really confused
– Oscar Arranz
Nov 7 at 23:08












1 Answer
1






active

oldest

votes

















up vote
0
down vote













In your Raton class, in moverse(), you have if gato.getmovimientos() % 3 == 0 is True, then this leads to changing movido from False to True, however you have no statement for when gato.getmovimientos() is False, which is why you have an infinite loop.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199022%2fpython-gets-into-an-infinite-loop-that-does-nothing%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    In your Raton class, in moverse(), you have if gato.getmovimientos() % 3 == 0 is True, then this leads to changing movido from False to True, however you have no statement for when gato.getmovimientos() is False, which is why you have an infinite loop.






    share|improve this answer



























      up vote
      0
      down vote













      In your Raton class, in moverse(), you have if gato.getmovimientos() % 3 == 0 is True, then this leads to changing movido from False to True, however you have no statement for when gato.getmovimientos() is False, which is why you have an infinite loop.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        In your Raton class, in moverse(), you have if gato.getmovimientos() % 3 == 0 is True, then this leads to changing movido from False to True, however you have no statement for when gato.getmovimientos() is False, which is why you have an infinite loop.






        share|improve this answer














        In your Raton class, in moverse(), you have if gato.getmovimientos() % 3 == 0 is True, then this leads to changing movido from False to True, however you have no statement for when gato.getmovimientos() is False, which is why you have an infinite loop.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 0:47









        public static void main

        8261621




        8261621










        answered Nov 7 at 23:43









        F. Montes

        64




        64






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199022%2fpython-gets-into-an-infinite-loop-that-does-nothing%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings