Created
September 23, 2017 18:17
-
-
Save tejuafonja/38d9fa40c99a6a0f44a016cf589e73df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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
credit: https://stackoverflow.com/questions/33508034/making-a-collatz-program-automate-the-boring-stuff