Home > Net >  Unable to find a way to break a sequence
Unable to find a way to break a sequence

Time:04-30

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()

CodePudding user response:

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()

CodePudding user response:

change your code to:

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)
       if len(List)<len(x):
           break

   print(List)
seq()
  • Related