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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x