# How to use recursion in Python?

Good evening.
Know what to lay out large pieces of code - it's disgusting, but otherwise will not work. There is a function:
def check_possibility(figures, signs = [], is_100 = False):
signs.append(sign)
if len(signs) == 5:
numeric_expression = "
numeric_expression += figures[0]
for i in range(5):
numeric_expression += signs[i]
numeric_expression += figures[i + 1]
result = eval(numeric_expression)
if result == 100:
return numeric_expression
else:
return False
elif is_100 == False:
is_100 = check_possibility(figures, signs, is_100)
return is_100

It takes a list of 6 digits (the digits in the string representation).
On the idea of this function must use recursion to try all possible combinations of signs (or their absence), to place between the numbers and check, is obtained expression 100.
The interpreter returns this:
Traceback (most recent call last):
File "D:\Дмитрий\Documents\Python\programs\def.py", line 46, in
main()
File "D:\Дмитрий\Documents\Python\programs\def.py", line 42, in main
numeric_expression = check_possibility(number_list)
File "D:\Дмитрий\Documents\Python\programs\def.py", line 32, in check_possibility

The last line is duplicated endlessly.
I suspect that this function has the wrong. Could you explain me how to create the correct feature (or fix old).
July 9th 19 at 11:19
July 9th 19 at 11:21
Solution
def check_possibility(figures, signs = [], is_100 = False):
new_signs = signs[:]
new_signs.append(sign)
if len(signs) == 5:
numeric_expression = "
numeric_expression += figures[0]
for i in range(5):
numeric_expression += signs[i]
numeric_expression += figures[i + 1]
result = eval(numeric_expression)
if result == 100:
return numeric_expression
else:
return False
elif is_100 == False:
is_100 = check_possibility(figures, new_signs, is_100)
return is_100

check_possibility print([str(n) for n in range(2, 8)])

ps. ** the dangerous operation will be very long to count if you got a large case. python believes in long arithmetic for integers.
Now the function correctly handles cases where it is impossible to 100 and returns False, but if you can get 100, there is still the same error. - hilbert_Hermann commented on July 9th 19 at 11:24
: put the full code that works for me. - chaz_Wiso commented on July 9th 19 at 11:27
: Yes, yet not recommended setting default = []. https://habrahabr.ru/post/50381/ - chaz_Wiso commented on July 9th 19 at 11:30
: just like a dict and object - marilyne_Roh commented on July 9th 19 at 11:33
July 9th 19 at 11:23
signs is passed as copy or by reference?
Take a look under the debugger,and the code is good, and have launched, (can't yet)
In this code, don't see too much a different sequence of those characters

Find more questions by tags ProgrammingRecursionPython