ABSP, Ch 3, Practice Project

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s