-
-
Save Neemox/4122615b6e38c83f4818079fc2c97f83 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def get_number() | |
while True: #(!num) through a big error | |
try: | |
print('Please enter a number for the Collatz sequence:') | |
num = int(input()) # Prompt for a number | |
return num | |
break | |
except ValueError: # If we couldn't make it an int... | |
print("enter an integer") # ... complain | |
def collatz(n): | |
# return next number after n in the collatz sequence | |
if n % 2 == 0: | |
n = n // 2 | |
print(n) | |
return n | |
else: | |
n = n * 3 + 1 | |
print(n) | |
return n | |
def main(): | |
number = get_number() | |
while number != 1: | |
number = collatz(number) | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment