Python Forum
Code Change Help: Infinite Loop Error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Code Change Help: Infinite Loop Error (/thread-16685.html)



Code Change Help: Infinite Loop Error - bindulam - Mar-10-2019

[Image: m1ExX.png]

# coding: utf-8

# In[1]:


import queue

import sys
N = int(input())
K = int(input())


# In[2]:


Ar = []
# for i in range(0,5):
#     a = int(input())
i = sys.stdin.readline()
A = i.split()


# In[4]:


print(A)


# In[6]:


Q = queue.Queue(maxsize = N)
St = queue.LifoQueue(maxsize=N)


# In[7]:


def subarraylen(arr , n ,x):
    length = n +1
    for i in range(0,n): 
        arr_sum = arr[i] 
        if (arr_sum > x): 
            return 1
   
        for j in range(i+1,n): 
            arr_sum += arr[j] 
            if arr_sum >= x and (j - i +1 ) < length: 
                length = (j - i + 1 ) 
          
    return length;


# In[8]:


import sys
L = []
i = 1
while(i == 1):
    print(i)
    for i in range(0,len(A)):
        Q.put(int(A[i]))
    for  i in range(0,len(A)):
        L.append(Q.get())
    for i in range(0,len(A)):
        St.put(int(A[i]))
    for i in range(0,len(A)):
        L.append(St.get())
    if(sys.getsizeof(L) >= 50):
	print("hi")
        i = 0


# In[19]:

le = subarraylen(L,3*N,K)
print(le)
Facing Infinite Loop Error in my code.

Code and Problem Statement Link here: https://github.com/bindulam/Python-Sum-if/blob/master/code%20v1.py


RE: Code Change Help: Infinite Loop Error - woooee - Mar-10-2019

print("hi")
is indented incorrectly which means you will get an "expected indented block" error. The program will exit and will never reach "i = 0". Also, you should get in the habit of putting a space after the if
if(sys.getsizeof(L) >= 50):



RE: Code Change Help: Infinite Loop Error - hshivaraj - Mar-10-2019

Your problem lies in this here in the if block. In order to exit the loop you need i to be 0.
    if(len(L) >= 50):
        print("hi")
        i = 0
And also you could use just the len function to find the length of the list.