I've got some homework on recursion, and I'm really stuck. The task is to create a program where we enter upper and lower boundaries and a number within those boundaries. The program should compute the middle number of those boundaries. If the middle number is bigger than the number, the middle number becomes the lower boundary, if lower, the upper one. It does it till it picks the right number. The moment it is the same, the program stops and returns it.
Here is the code that I've already written, but it only works when the 1st guess of the number is right:
def number_guess(number ,upper_boundary, lower_boundary):
boundary_dif = upper_boundary - lower_boundary
middle_number = boundary_dif/2 + lower_boundary
if middle_number == number:
return middle_number
if middle_number > number:
middle_number = lower_boundary
number_guess(number, upper_boundary, lower_boundary)
if middle_number < number:
middle_number = upper_boundary
number_guess(number, upper_boundary, lower_boundary)
PS It must be done only with recursion.
Recursion can take awhile to get a hold of. I would look into binary search to see how this works:
def number_guess(number, lower, upper):
middle = ((upper - lower) // 2) + lower
if middle == number:
return middle
if number > middle:
return number_guess(number, middle, upper)
return number_guess(number, lower, middle)
print(number_guess(57, 0, 100))
There are lots of issues with this small piece of code. First is a common recursion programming error. Your recursive function returns a value:
if middle_number == number:
return middle_number
But when you call it recursively, you ignore that returned value:
if middle_number > number:
middle_number = lower_boundary
number_guess(number, upper_boundary, lower_boundary)
If you return a value, you have to deal with it. In this case, the solution is simple, simply return it in turn:
return number_guess(number, upper_boundary, lower_boundary)
The next problem is the two limit tests are backwards:
if middle_number > number:
middle_number = lower_boundary
if the middle number is greater than the number, then it should be the new upper boundary, not lower. Ditto for the upper_boundary
test. Next, this assignment is backward:
middle_number = lower_boundary
return number_guess(number, upper_boundary, lower_boundary)
We don't use middle_number
again after this, we recurse using lower_boundary
so we should either do:
lower_boundary = middle_number
return number_guess(number, upper_boundary, lower_boundary)
or, we can simply do:
return number_guess(number, upper_boundary, middle_number)
ditto for the upper_boundary
code. Next, you tagged this as Python 3 so this code:
boundary_dif/2
will generate a floating point number, which you don't want! You need:
boundary_dif // 2
Finally, although this works:
boundary_dif = upper_boundary - lower_boundary
middle_number = boundary_dif // 2 + lower_boundary
it can also be expressed as simply:
middle_number = (upper_boundary + lower_boundary) // 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.