[英]BinarySearch missing required positional arguments
抱歉 - 对 Python 来说还是新手,但我的代码无法正常工作。 我真的不知道在哪里移动声明的变量并让它工作。 但是,如果我将它们放在函数之外,其他函数将被忽略并直接进入 n 的输入请求。 有任何解决这个问题的方法吗?
编辑:添加了整个代码以及我在底部遇到的错误,我不知道这是否是一个简单的缩进错误。
# 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'
运行BinarySearch函数时,您应该传递n、sortedlist、位置变量。
如果1.和2.不修改,代码会陷入死循环。
如果你将它应用到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.