BinarySearch missing required positional arguments

Sorry – still new to Python, but having trouble with my code working. I can’t really figure out where to move the declared variables and getting it to work. However, if I put them outside the function, the other functions are ignored and go straight to n’s input request. Any way to fix this?

EDIT: Added the entire code and also the errors I am getting at the bottom, I don’t know if this is a simple indentation error.

# menu selector
from tkinter.tix import Select


select = 0
def DisplayMenu() :
    print ("enter your choice")
    print ("1 for a Linear Search")
    print ("2 for a Binary Search")
    print ("3 for a Bubble Sort")
    print ("4 for a Selection Sort")
    print ("5 for a Insertion Sort")
def SelectRoutine() :
    global select
    DisplayMenu()
    select = int(input())
    if (select == 1) :
        print ("Call the Linear Search Routine")
        LinearSearch()
    elif (select == 2) :
        print ("Call the Binary Search Routine")
        BinarySearch()
    elif (select == 3) :
        print ("Call the Bubble Sort Routine")
        BubbleSort()
    elif (select == 4) :
        print ("Call the Selection Sort")
        SelectionSort()
    elif (select == 5):
        print ("Call the Insertion Sort")
        InsertionSort()
    else :
        print("invalid selection")
def LinearSearch() :
    elements = [10, 20, 80, 70, 60, 50]
    x = int(input("please enter the number to search: "))
    found = False
    for i in range(len(elements)) :
        if(elements[i] == x) :
            found = True
            print("%d found at %dth position" % (x, i))
            break
    if (found == False) :
            print("%d is not in list" % x)
def BinarySearch(n, sortedlist, x) :
    start = 0
    end = n - 1
    for i in range(n) :
        sortedlist.append(int(input("Enter %dth element: " % i)))
    while(start <= end) :
        mid = int((start + end) / 2)
    if (x == sortedlist[mid]) :
        return mid
    elif(x < sortedlist[mid]) :
        end = mid - 1
    else :
        start = mid + 1
        return -1
    n = int(input("Enter the size of the list: "))
    sortedlist = []
    x = int(input("Enter the number to search: "))
    position = BinarySearch(n, sortedlist, x)
    if (position != -1) :
        print("element number %d is present at position: %d" % (x,position))
    else :
        print("element number %d is not present in the list" % x)
SelectRoutine()
enter your choice
1 for a Linear Search
2 for a Binary Search
3 for a Bubble Sort
4 for a Selection Sort
5 for a Insertion Sort
2
Call the Binary Search Routine
Traceback (most recent call last):
  File "/Users/uglycode.py", line 68, in <module>
    SelectRoutine()
  File "/Users/uglycode.py", line 22, in SelectRoutine
    BinarySearch()
TypeError: BinarySearch() missing 3 required positional arguments: 'n', 'sortedlist', and 'x'

Actual

>Solution :

You should pass n, sortedlist, position variables when you run BinarySearch function.

  1. You should define variables n, sortedlist, position from out of the BinarySearch function.
  2. Your indent under while loop is wrong. You should run binary search logic under your while function.
  3. If you want to run BinarySearch function when you want, make it all to one function as run_binary_search()

If 1. and 2. are not modified, the code will fall into an infinite loop.

If you apply it to SelectRoutine() then, you should define def BinarySearch() function like it.

def BinarySearch():
    def _binary_serach(n, sortedlist, x):
        start = 0
        end = n - 1
        while (start <= end) :
            mid = int((start + end) / 2)
            if (x == sortedlist[mid]):
                position = mid
                break
            elif(x < sortedlist[mid]):
                end = mid - 1
            else:
                start = mid + 1
                position = -1
        if (position != -1) :
            print("element number %d is present at position: %d" % (x,position))
        else :
            print("element number %d is not present in the list" % x)
        return position

    n = int(input("Enter the size of the list: "))
    sortedlist = []
    for i in range(n):
        sortedlist.append(int(input("Enter %dth element: " % i)))
    x = int(input("Enter the number to search: "))
    position = _binary_serach(n, sortedlist, x)
    return position

Leave a Reply