Advertisement
mengyuxin

meng.collatz.py

Dec 29th, 2017
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. def collatz(num):
  2.     while num > 1:
  3.         print(num)
  4.         if num % 2 == 0:
  5.             num = int(num // 2)
  6.         else:
  7.             num = int(3 * num + 1)
  8.     else:
  9.         print(num)
  10.         #print('Finished.')
  11.  
  12. def main():
  13.     myInput = input('Please enter an integer: ')
  14.     try:
  15.         num = int(myInput)
  16.         collatz(num)
  17.     except:
  18.         print('ValueError: invalid input. You entered ' + myInput + '.')      
  19.    
  20. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement