My solution to the Practice Project in Chapter 3 of the excellent book “Automate the Boring Stuff with Python” (ABSP)
#! usr/bin/env python3 # by lorenzo - 06/08/2017 # the Collatz sequence def collatz(number): if number % 2 == 0: result = number // 2 print(result) return result if number % 2 == 1: result = 3 * number + 1 print(result) return result print('Enter an integer:') try: anumber = int(input()) except ValueError: print('You have to enter an integer!') while anumber != 1: anumber = collatz(anumber)
Comments about the code are welcome.