简体   繁体   English

BinarySearch 缺少所需的位置参数

[英]BinarySearch missing required positional arguments

Sorry - still new to Python, but having trouble with my code working.抱歉 - 对 Python 来说还是新手,但我的代码无法正常工作。 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.但是,如果我将它们放在函数之外,其他函数将被忽略并直接进入 n 的输入请求。 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实际的

You should pass n, sortedlist, position variables when you run BinarySearch function.运行BinarySearch函数时,您应该传递n、sortedlist、位置变量。

  1. You should define variables n, sortedlist, position from out of the BinarySearch function.您应该在BinarySearch函数之外定义变量n、sortedlist、位置
  2. Your indent under while loop is wrong.您在while 循环下的缩进是错误的。 You should run binary search logic under your while function.您应该在while函数下运行二进制搜索逻辑
  3. If you want to run BinarySearch function when you want, make it all to one function as run_binary_search()如果您想在需要时运行BinarySearch函数,请将其全部作为run_binary_search()函数

If 1. and 2. are not modified, the code will fall into an infinite loop.如果1.和2.不修改,代码会陷入死循环。

If you apply it to SelectRoutine() then, you should define def BinarySearch() function like it.如果你将它应用到SelectRoutine()那么你应该定义def BinarySearch()函数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM