Home > Software design >  Python - I can't find a way to break the function
Python - I can't find a way to break the function

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

Thank you!

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

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

  • Related