In the case of non-integer input, the program will fail before you call collatz()with the code as you wrote it, which means that your block trywill not catch the exception:
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)
try/except. collatz():
def collatz(y):
if y % 2 == 0:
print(int(y/2))
return int(y/2)
else:
print(int(y*3+1))
return int(y*3 +1)
print('Please enter a number and the Collatz sequence will be printed')
try:
x = int(input())
except ValueError:
print('Error: Invalid Value, the program should take in integers.')
exit()
while x != 1:
x = collatz(x)