Making a collatz program automate the boring stuff

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

    14 lines:

    Don't get why we need "elif number %2 == 1:" instead of simple 'else'?

    def  collatz(number):
        while number != 1:
            if number %2 == 0:
                number = number/2
                print(number)
            else:
                number = 3*number+1
                print(number)
    print('Enter a number')
    try:
        number = (int(input()))
    except ValueError:
              print("Please enter an INTEGER.")
    collatz(number)
    

提交回复
热议问题