AutomateTheBoringStuff.Ch03.Projects package¶
Submodules¶
AutomateTheBoringStuff.Ch03.Projects.P1_makeCollatzSeq module¶
Make Collatz Sequence
This program makes a Collatz Sequence for a given number.
Write a function named collatz()
that has one parameter named number.
If number is even, then collatz()
should print number // 2 and return this value.
If number is odd, then collatz()
should print and return 3 * number + 1.
Then write a program that lets the user type in an integer and that keeps calling
collatz()
on that number until the function returns the value 1.
Example
Enter number:
3
10
5
16
8
4
2
1
-
AutomateTheBoringStuff.Ch03.Projects.P1_makeCollatzSeq.
collatz
(number: int) → int[source]¶ Collatz
If number is even, then return number // 2. If number is odd, then return 3 * number + 1.
Parameters: number – Integer to generate a Collatz conjecture term for. Returns: Integer that is either a quotient or a product and sum.
AutomateTheBoringStuff.Ch03.Projects.P2_inputValidation module¶
Input validation
This program adds input validation to P01_make_collatz_seq
Add try and except statements to the previous project to detect whether
the user types in a noninteger string. Normally, the int
function will
raise a ValueError
error if it is passed a noninteger string, as in int(‘puppy’).
In the except clause, print a message to the user saying they must enter an integer.