I'm trying to write a Collatz sequence while adding a try and except statements to detect whether the user types in a noninteger string and I can't seem to figure out how to do so. If read Try/Except in Python: How do you properly ignore Exceptions?, but am still at a loss. My code so far:
def collatz(y):
try:
if y % 2 == 0:
print(int(y/2))
return int(y/2)
else:
print(int(y*3+1))
return int(y*3 +1)
except ValueError:
print('Error: Invalid Value, the program should take in integers.')
print('Please enter a number and the Collatz sequence will be printed')
x = int(input())
while x != 1:
x = collatz(x)