So to fix your code as described:
def collatz(number):
number = input() # number is passed in - doesn't make sense to get it again
try:
number = int(number) # Just interpret once
except ValueError: # try must have an except clause
return # exit on error
if number%2 == 0:
print('hi')
elif number%2 == 1: # Don't really need an elif - %2 can only have 2 values
print(number*3+1)
However, you may want to look into using a generator (yield
):
def collatz(number):
while number > 1:
if number % 2 == 0:
number //= 2
else:
number = 3*number + 1
yield number
def collatz_series():
try:
number = int(input())
except ValueError:
return
for n in collatz(number):
print(n)
0
solved invalid syntax on int(number) on a try statement python