Python Forum
Code Change Help: Infinite Loop Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code Change Help: Infinite Loop Error
#1
[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-i...de%20v1.py
Reply
#2
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):
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked Why this code does not go into an infinite loop? 2367409125 2 835 Dec-02-2022, 08:22 PM
Last Post: deanhystad
  Need help with infinite loop & making hotkeys/shortcuts Graxum 1 1,124 Aug-22-2022, 02:57 AM
Last Post: deanhystad
Photo (Beginners problem) Does file change during this code? fiqu 3 1,849 Nov-03-2021, 10:23 PM
Last Post: bowlofred
  Change the varaible in For Loop quest 2 1,680 Aug-13-2021, 03:35 PM
Last Post: deanhystad
  How do I change this code for searching duplicats in python ? Eidrizi 0 1,486 Mar-17-2021, 01:34 PM
Last Post: Eidrizi
  Infinite loop problem Zirconyl 5 2,923 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  Change variable value during a while loop? penahuse 2 3,989 Nov-15-2020, 11:53 PM
Last Post: penahuse
  using 'while loop' output going into infinite loop... amitkb 2 1,910 Oct-05-2020, 09:18 PM
Last Post: micseydel
  Infinite loop not working pmp2 2 1,608 Aug-18-2020, 12:27 PM
Last Post: deanhystad
  Differential equations with initial condition in Python (change a working code) Euler2 1 1,793 May-29-2020, 04:06 PM
Last Post: Euler2

Forum Jump:

User Panel Messages

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