Making a collatz program automate the boring stuff

前端 未结 25 2325
一整个雨季
一整个雨季 2020-12-08 11:14

I\'m trying to write a Collatz program using the guidelines from a project found at the end of chapter 3 of Automate the Boring Stuff with Python. I\'m using python 3.

25条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 11:36

    def collatz(number):
        if(number%2==0):
            n=number//2
            print(n)
            return n
        else:
            ev=3*number+1
            print(ev)
            return ev
    num1=input("Enter a number: \n")
    
    try:
        num= int(num1)
        if(num==1):
            print("Enter an integer greater than 1")
        elif(num>1):
            a=collatz(num) 
            while(True):
                if(a==1):
                    break
                else:
                    a=collatz(a)
        else:
            print("Please, Enter a positive integer to begin the Collatz sequence")
    
    except:
        print("please, Enter an integer")
    

    Try to came up with a solution based on up to chapter Function from automate the boring stuff. If need help related to Collatz Problem, then visit here: http://mathworld.wolfram.com/CollatzProblem.html

提交回复
热议问题