Making a collatz program automate the boring stuff

前端 未结 25 2311
一整个雨季
一整个雨季 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:34

    def cyclic(number):
        if number % 2 == 0:
            if number // 2 == 1:
                print(1)
            else:
                print(number//2)
                cyclic(number // 2)
        else:
            print((number * 3) + 1)
            cyclic((number * 3) + 1)
    
    
    print("Enter a number:")
    try:
        n = int(input())
        cyclic(n)
    except ValueError:
        print("Unvalied value")
    

    The simplest one

提交回复
热议问题