Python Automate the Boring Stuff Collatz exercise I'm looking for some feedback on my code for the collatz function exercise in the Python book Automate the Boring Stuff. It works, but doesn't feel very elegant to me. Is there a better way to approach it? I'm trying to avoid just looking at someone else's solution so general advice would be preferred.

def collatz(number):    
 if (number %2 ==0 ) :#even
     newNumber=number//2
     print(newNumber);
     if newNumber!=1:
         collatz(newNumber);
     else:
         return (newNumber)
 elif (number % 2 == 1) : #odd
     newNumber=3*number+1
     print (newNumber)
     collatz(newNumber)
     return newNumber;


#Input validation loop
while True:
    try:
        number = int(input("Enter number:"))    
    except ValueError:
        print("Enter a valid integer")
        continue
    else:
        if number &lt; 1:
            print("enter a valid positive integer")
            continue
        break

collatz(number)
Some quick thoughts:


Linting the code will make it more easily readable to Python developers (including yourself when you get used to it).
elif (number % 2 == 1) : could just be else: because a number which is not even must be odd.
return (newNumber) should be just return newNumber.
The code below #Input validation loop should be in a main function, called if __name__ == '__main__'. That way it's possible to import your function for reuse in other code.
Rather than returning just the next number in the sequence you probably want to return an infinite iterator so that the caller can get more numbers trivially:

class Collatz():
    def __init__(self, start):
        self.number = start

    def __iter__(self):
        return self

    def __next__(self):
        if self.number in [0, 1]:
            raise StopIteration()
        if self.number % 2 == 0:
            self.number = int(self.number / 2)
        else:
            self.number *= 3
            self.number += 1
        return self.number


Use:

&gt;&gt;&gt; print(list(Collatz(15)))
[46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]