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.
My 17 lines of code for the same exercise that I have came up with.
def collatz(number):
""" check if the number is even or odd and performs calculations.
"""
if number % 2 == 0: # even
print(number // 2)
return number //2
elif number % 2 != 0: # odd
result = 3*number+1
print(result)
return result
try:
n = input('Enter number: ') # takes user input
while n !=1: # performs while loop until 'n' becomes 1
n = collatz(int(n)) # passes 'n' to collatz() function until it arrives at '1'
except ValueError:
print('Value Error. Please enter integer.')