``````f = open("summatest.01.sis", "rt")

f.close()

# Valjund
vfal = file("sum.val", "w")

lmargid = []

def otsi(k):
if (k < n):
print n
k = k + 1
lmargid.append("+")
if otsi(k):
return True
lmargid.append("-")
print lmargid
else:
for i in range(n + 1):
if lmargid.index(i) == "+":
else:
if z == otstav_summa:
for ii in xrange(n + 1):
vfal.write("=" + z)
return True
return False

if not otsi(0):
vfal.write("EI OLE")``````
``````File "summa.py", line 17, in otsi
if (k < n):
RuntimeError: maximum recursion depth exceeded in cmp``````

Input:
summa.sis
3
15 25 30
10
Output:
summa.val
15+25-30=10

3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by woooee

You are comparing the generator n to integer instead of reading values from it, I do not understand. Mitä häh?

Ok, that was not generator, but unnecessarry parenthesis at line 2. Still strange:

``````f = open("summatest.01.sis", "rt")

f.close()

# Valjund
vfal = file("sum.val", "w")

lmargid = []

def otsi(k):
if (k < n): # n is the first line of file f,
print n # n is after k in alphabetic order or
## allways true if k is number and n is string ('number' < 'string')
k = k + 1 # ??? k is number?
lmargid.append("+")
if otsi(k): # recursion grown k,
return True
lmargid.append("-")
print lmargid
else: #
for i in range(n + 1):
if lmargid.index(i) == "+":
else:
if z == otstav_summa:
for ii in xrange(n + 1):
vfal.write("=" + z)
return True
return False

if not otsi(0):
vfal.write("EI OLE")``````

Debug with lines 19 and 20 commented out. Kommentoi pois rivit 19 ja 20 ja etsi virhe.

Edited by pyTony: n/a

Assuming k is an integer, it will never be less than "n" because "n" is a string, as already stated. Print both "k" and "n" to test.

``````def otsi(k):
if (k < n):
print "k < ", k, n
#
#     instead of recursion you should be using a while loop
#     recursion is rarely, if ever, used in actual code
def otsi(k):
##  while (k < n):
##  or even better
for k in range(n):
print k, n
lmargid.append("+")
lmargid.append("-")
print lmargid
#
# or even, even better
lmargid.append("+-"*n)``````

Edited by woooee: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.