gpt4 book ai didi

Why am I getting this error? "name 'get_sorted_run_length' is not defined" [closed](为什么我会收到这个错误?“未定义名称‘GET_SORTED_RUN_LENGTH’”[CLOSED])

转载 作者:bug小助手 更新时间:2023-10-24 23:15:37 31 4
gpt4 key购买 nike




Im just trying to call the function get_sorted_run_length from another function called natural_merge_sort, but when I call it. I get the error "name 'get_sorted_run_length' is not defined".
This is how I am calling it:

我只是尝试从另一个名为Natural_Merge_Sort的函数调用Get_sorted_Run_Length函数,但当我调用它时。我收到错误消息“未定义名称‘GET_SORTED_RUN_LENGTH’”。我是这样称呼它的:


def natural_merge_sort(self, integer_list):
get_sorted_run_length(integer_list, 0)

This is the traceback:

这是回溯:


Traceback (most recent call last):
File "main.py", line 50, in <module>
main()
File "main.py", line 43, in main
sorter.natural_merge_sort(list5_copy)
File "/home/runner/local/submission/NaturalMergeSorter.py", line 17, in natural_merge_sort
get_sorted_run_length(integer_list, 0)
NameError: name 'get_sorted_run_length' is not defined

I only have one local variable on the get_sorted_run_length function and tried to put ir global but that didn't do anything.

我在GET_SORTED_RUN_LENGTH函数上只有一个局部变量,并试图将ir设置为全局变量,但这并没有起到任何作用。


I have 3 files, main.py, RunLengthTestCase.py, and NaturalMergeSorter.py. I think that the error is in the NaturalMergerSorter.py

我有3个文件,main.py、RunLengthTestCase.py和NaturalMergeSorter.py。我认为错误出在NaturalMergerSorter.py中


The full code for that file is:

该文件的完整代码为:


class NaturalMergeSorter:
def __init__(self):
return

def get_sorted_run_length(self, integer_list, start_index):
counter = 1
for i in range(start_index, len(integer_list)-1):
if integer_list[i] <= integer_list[i+1]:
counter += 1
else:
return counter
if(counter == 1):
counter = 0
return counter # Modify this line.

def natural_merge_sort(self, integer_list):
get_sorted_run_length(integer_list, 0)
index = 0
index = get_sorted_run_length(self,integer_lsit,index)
if index == len(integer_list):
return integer_list
i = 0
else:
get_sorted_run_length(integer_list, index)
merge()

return integer_list # Comment out this line.

def merge(self, numbers, left_first, left_last, right_last):
merged_size = right_last - left_first + 1

merged_numbers = [None] * merged_size
merge_pos = 0
left_pos = left_first
right_pos = left_last + 1

# Add smallest element from left or right partition to merged numbers
while left_pos <= left_last and right_pos <= right_last:
if numbers[left_pos] <= numbers[right_pos]:
merged_numbers[merge_pos] = numbers[left_pos]
left_pos += 1
else:
merged_numbers[merge_pos] = numbers[right_pos]
right_pos += 1

merge_pos += 1

# If left partition isn't empty, add remaining elements to merged_numbers
while left_pos <= left_last:
merged_numbers[merge_pos] = numbers[left_pos]
left_pos += 1
merge_pos += 1

# If right partition isn't empty, add remaining elements to merged_numbers
while right_pos <= right_last:
merged_numbers[merge_pos] = numbers[right_pos]
right_pos += 1
merge_pos += 1

# Copy merged numbers back to numbers
for merge_pos in range(merged_size):
numbers[left_first + merge_pos] = merged_numbers[merge_pos]

更多回答

Without seeing your code -- in particular, where get_sorted_run_length is defined in relation to natural_merge_sort -- we won't be able to help you.

如果看不到您的代码--特别是其中的get_sorted_run_length是相对于Natural_merge_sort定义的--我们将无法帮助您。

Is get_sorted_run_length defined in your class or outside of your class?

GET_SORT_RUN_LENGTH是在您的类中定义的,还是在您的类之外定义的?

I have added the full code for the file where I think the error is happening

我已经为我认为发生错误的文件添加了完整的代码

get_sorted_run_length() is an attribute of the class, so you need to put self. in front of it. self.get_sorted_run_length()

GET_SORTED_RUN_LENGTH()是类的一个属性,所以您需要放入self。在它的前面。Self.get_sorted_run_long()

You don't need to pass self as an actual argument. Python takes care of that automatically. Just call it with two arguments.

您不需要将self作为实际参数传递。Python会自动处理这一问题。只要有两个论点就可以了。

优秀答案推荐

Solution:

解决方案:


You have to add 'self' to your method calls if they are defined inside your class.

如果方法调用是在类中定义的,则必须将它们添加到方法调用中。


self.get_sorted_run_length(integer_list, 0)

This let's python know to look within the current class for the function definition, otherwise it's searching for it defined globally, and it's not there.

这让Python知道要在当前类中查找函数定义,否则它将搜索全局定义的函数,但它不在那里。


更多回答

So for every function that has self as a parameter, I have to call them like that?

所以对于每个以self为参数的函数,我必须这样调用它们吗?

Yes, you do. It defines the scope just like globally and locally Python needs to know where to look for it.

不,你有。它定义作用域,就像在全局和本地一样,Python需要知道在哪里查找它。

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com