Skip to content

Instantly share code, notes, and snippets.

@tejuafonja
Created September 23, 2017 18:17
Show Gist options
  • Save tejuafonja/38d9fa40c99a6a0f44a016cf589e73df to your computer and use it in GitHub Desktop.
Save tejuafonja/38d9fa40c99a6a0f44a016cf589e73df to your computer and use it in GitHub Desktop.
# python 2.7
def collatz(number):
if number % 2 == 0:
print number // 2
return number // 2
elif number % 2 == 1:
result = (3 * number + 1)
print result
return result
def run_collatz():
try:
n = input("Give me a number: ")
if isinstance(n, (int,float)):
n = int(n)
while n != 1:
n = collatz(n)
elif isinstance(n, (str)):
print n," is a string, give me a number"
run_collatz()
else:
print ("Please input a valid number")
run_collatz()
except NameError:
print "oops! that doesn't look like a number"
run_collatz()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment