Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – I can't find a way to break the function

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading