Use a sys.setrecursionlimit
to stop the 999 from repeating forever. Here is an edited version of your code. I put a sys.argv[]
as well, just for fun because I imported sys
. The sys.argv[]
gets the argument after you run your program like this: python myscript.py 13.5
In this, sys.argv[1]
is 13.5
. I set the recursion limit pretty high, but the downfall of this is that the Segmentation Fault: 22
still occurs, and I haven't found a way to get rid of that.
import time
import sys
while True:
try:
var = int(sys.argv[1])
break
except IndexError:
print 'You have to enter a number!'
var = int(raw_input('Enter a number: '))
break
def check(x):
z = x
try:
while z != 1:
if isEven(z) == True:
z = z/2
print z
else:
z = (3*z)+1
print z
else:
print 'Got one!'
x = x+1
print 'Trying for %d...' %(x)
check(x)
except:
print 'You were at number %d' %(x)
check(x)
def isEven(number):
return number % 2 == 0
sys.setrecursionlimit(10000000)
check(var)