0

I wanted to write a recursive function to fit curves. For some reasons my results didn't looked good so I boiled down the problem to one easy example:

Number = 6
Successor = 0

def Fuction(Number, Successor):
    if (Number - Successor)+1>0:
       Successor += 1
       Fuction(Number, Successor)
    else:
        return(Fuction(Number, Successor))

A = Fuction(Number, Successor) 
print(f'The Successor of {Number} is {A}')

When I do this I get the output

7
The Successor of 6 is 1

How can I fix this?

Phicalc
  • 75
  • 1
  • 6
  • Why `print(Successor)` rather than `return Successor` in the basis case? Also -- the output you report doesn't match the code you posted. – John Coleman Jun 02 '21 at 13:23
  • I changed the output. – Phicalc Jun 02 '21 at 13:29
  • In addition to having `print` when a `return` makes more sense, you aren't *returning* the result `Function(Number, Successor)` in the recursive branch. Also -- your naming conventions clash with the commonly used [pep style guide](https://www.python.org/dev/peps/pep-0008/) – John Coleman Jun 02 '21 at 13:35
  • I changed the code after according to . But now the Kernel dies – Phicalc Jun 02 '21 at 13:40
  • If you changed the code and are getting a completely different error, perhaps you could write a new question. – John Coleman Jun 02 '21 at 14:00

0 Answers0