pythonlistfunctioncollatz

Unable to find a way to break a sequence


I wanted the sequence to break (not to print any sequence) if the length of the name is greater than the length of the sequence but I couldn't find a way: I know that the print(List) statement shouldn't be there but i don't know where to put it.

x=input("enter name")
def seq():
   q=1
   n=int(input("enter number"))
   List=[n]
   while q<n:
       if (n % 2):
           n = 3*n + 1
           List.append(n)
       else:
           n=n//2
           List.append(n)
   while len(List)<len(x):
       break

   print(List)
seq()

Solution

  • Try this by replacing when with If Condition and also you can try passing x value to fun:

    
    def seq():
       q=1
       n=int(input("enter number"))
       List=[n]
       while q<n:
           if (n % 2):
               n = 3*n + 1
               List.append(n)
           else:
               n=n//2
               List.append(n)
       If (len(List)<len(x)):
           break
    
       print(List)
    
    x=input("enter name")
    seq()