1#!/usr/bin/env python2#Coding:utf-83#Write by Mfyang4#collatz.py5#reads a value from the user and determines whether the value is an integer of type int, if not given an exception hint6#If this value is even then let this number//27#If this value is odd then let this number x3 + 18#by constantly invoking the return value of the function and printing the return value until the return value is 19 num = raw_input ("Please input a number:") 11#except Nameerror:12#print "Your input not a number"13 14defCollatz (num):15ifNum% 2 = =0:16returnNum//2 17Else: 18returnnum * 3 + 1 19returnNumIsnumber = 1 22Try: num =int (num)24exceptValueError:25Print "Your input not a nubmer"Isnumber =027 whileNum! = 1: 28ifIsnumber = = 1: num =Collatz (num)30PrintNum31Else: 32 Break
Python Programming Quick Start 3rd Chapter Practical Project Reference Answer