如何在Python中调用类方法?

如何在Python中调用类方法?,python,Python,我的代码显示未定义二进制搜索,我尝试为其创建一个变量,但仍然无法工作 class Queue: def __init__(self): print("Welcome to shirley, you can check on our menu and select what you want to do xx") self.queue = ["gym", "bar", "restaur

我的代码显示未定义二进制搜索,我尝试为其创建一个变量,但仍然无法工作

class Queue:

    def __init__(self):
        print("Welcome to shirley, you can check on our menu and select what you want to do xx")

        self.queue = ["gym", "bar", "restaurant", "store", ]

    def binary_search(self, target, low, high):

        # self=self.queue
        while low <= high:

            # self.queue = ["gym","bar","restaurant", "store",]
            mid = low + ((high - low) // 2)

            if self.queue[mid] == target:
                return self.queue[mid], mid

            elif self.queue[mid] > target:
                high = mid - 1

            else:
                low = mid + 1
        return -1
        # the end minus 1 (meaning we've got nowhere else
        # to look for), work out the middle of the array
        # and see if the number is present.

        result = binary_search(self.queue, target, 0, len(self.queue) - 1)


        if result != -1:
            print("Element is present at index " + str(result))
        else:
            print("Not found")

    def program(self):

        print("enter your search item")
        target = input()
        binary_search(target, 0, len(self.queue)-1)


q = Queue()

q.program()
由于二进制搜索定义为绑定函数,因此在调用该函数之前需要添加self

在二进制搜索函数中,在程序到达

        if result != -1:
            print("Element is present at index " + str(result))
        else:
            print("Not found")
部分。看起来你要做的是定义另一个函数,它接受一个数字结果, 如果结果不是-1,则在index+strresult处存在prints out元素,否则未找到print

注意:

class Queue:

    def __init__(self):
        print("Welcome to shirley, you can check on our menu and select what you want to do xx")
        self.queue = ["gym", "bar", "restaurant", "store", ]

    def binary_search(self, target, low, high):
        # self=self.queue
        while low <= high:
            # self.queue = ["gym","bar","restaurant", "store",]
            mid = low + ((high - low) // 2)
            if self.queue[mid] == target:
                return self.queue[mid], mid
            elif self.queue[mid] > target:
                high = mid - 1
            else:
                low = mid + 1
        return -1

    def display(self, number):
        # the end minus 1 (meaning we've got nowhere else
        # to look for), work out the middle of the array
        # and see if the number is present.
        result = number
        if result != -1:
            print("Element is present at index " + str(result))
        else:
            print("Not found")

    def program(self):
        print("enter your search item")
        target = input()
        self.display(self.binary_search(target, 0, len(self.queue)-1))

test = Queue()
test.program()
输出:

Element is present at index ('bar', 1)

二进制搜索是一种类方法。你需要使用self.binary\u搜索…嗨,如果我使用self.binary\u搜索。。。它不会执行代码,它会修复错误。您的算法可能还有其他问题。
class Queue:

    def __init__(self):
        print("Welcome to shirley, you can check on our menu and select what you want to do xx")
        self.queue = ["gym", "bar", "restaurant", "store", ]

    def binary_search(self, target, low, high):
        # self=self.queue
        while low <= high:
            # self.queue = ["gym","bar","restaurant", "store",]
            mid = low + ((high - low) // 2)
            if self.queue[mid] == target:
                return self.queue[mid], mid
            elif self.queue[mid] > target:
                high = mid - 1
            else:
                low = mid + 1
        return -1

    def display(self, number):
        # the end minus 1 (meaning we've got nowhere else
        # to look for), work out the middle of the array
        # and see if the number is present.
        result = number
        if result != -1:
            print("Element is present at index " + str(result))
        else:
            print("Not found")

    def program(self):
        print("enter your search item")
        target = input()
        self.display(self.binary_search(target, 0, len(self.queue)-1))

test = Queue()
test.program()
Welcome to shirley, you can check on our menu and select what you want to do xx
enter your search item
bar
Element is present at index ('bar', 1)