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
  Infinite Loop with my code TheGrado 9 166 Mar-24-2026, 06:16 AM
Last Post: Pedroski55
  How to convert while loop to for loop in my code? tatahuft 4 1,659 Dec-21-2024, 07:59 AM
Last Post: snippsat
Shocked Why this code does not go into an infinite loop? 2367409125 2 2,225 Dec-02-2022, 08:22 PM
Last Post: deanhystad
  Need help with infinite loop & making hotkeys/shortcuts Graxum 1 2,548 Aug-22-2022, 02:57 AM
Last Post: deanhystad
Photo (Beginners problem) Does file change during this code? fiqu 3 3,246 Nov-03-2021, 10:23 PM
Last Post: bowlofred
  Change the varaible in For Loop quest 2 2,892 Aug-13-2021, 03:35 PM
Last Post: deanhystad
  How do I change this code for searching duplicats in python ? Eidrizi 0 2,232 Mar-17-2021, 01:34 PM
Last Post: Eidrizi
  Infinite loop problem Zirconyl 5 5,471 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  Change variable value during a while loop? penahuse 2 6,181 Nov-15-2020, 11:53 PM
Last Post: penahuse
  using 'while loop' output going into infinite loop... amitkb 2 3,402 Oct-05-2020, 09:18 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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