Home > Blockchain >  'NameError' Calling an existing function in a new one
'NameError' Calling an existing function in a new one

Time:02-05

I created a class containing several functions, I have an issue of name error whenever I try to call one of the functions in another one. In this case, the 'Sorting_array' function is what I called most, but it throws a NameError for every function which it is being called. Below is the Python Code:

arr = [5, 1, 9, 7]

#Display initial list
print("The Initial Array is:", arr)

#All array operations in a class
class Allarrays:

  def Check_Sort(self):
  #Check if the list is sorted or not
    global flag #this variable is reusable outside the function
    flag = 0
   i = 1
    while i < len(arr):
      if (arr[i] < arr[i - 1]):
      flag = 1
      i =1
    if (not flag):
    print("This array is sorted.")

    else:
    print("This array is unsorted, it needs to be sorted")

  def Sorting_array(self, arr):
  #sort unsorted list by swapping positions
    for i in range (len(arr)):
      for j in range(i 1, len(arr)):
        if arr[i] > arr[j]:
          arr[i], arr[j] = arr[j], arr[I]
    return arr

  def Not_Sorted(self):
    if flag:
    #This only displays this if the list is unsorted
      Sorting_array(arr) #Calling existing function for sorting array
      print("The sorted array is:", arr) #Display result

  def missing_element(self):
    global missing_numbers #this variable is reusable outside the function
    #Check for missing numbers in the list
    missing_numbers = [item for item in range(arr[0], arr[-1] 1) 
                if item not in arr]
    print("The following numbers are the missing elements:", missing_numbers)
    return arr

  def Concatenate_arrays(self):
    global m #this variable is reusable outside the function
    #Merging missing numbers to existing list
    m = arr   missing_numbers #List Concatenation
    Sorting_array(m) #Calling existing function 
               #for sorting list with a new parameter
    print("The Complete array with replaced missing elements", m) #Display result
    return m

  def Rvsal(self, m):
  #Reversing elements of the list by swapping the positions
    revs = m[::-1] #Swap positions of the elements 
    print("The reversed array", revs) #Display resulted array

  def nth_highest():
  #Checking the list for the second-highest number
  Sorting_array(m) #Sorting the list with an existing funtion and argument
  Second_highest = m[-2] #Assigning the second to the last element after sorting
  print("The second highest number of the array is:", Second_highest) 
                                                   #Displays result

  def Sum_Positive(self):
  #Sum of all positive numbers in the list
    pos = sum(x for x in m if x > 0) #Sum numbers that are greater than zero
    print("The sum of all positive number in the complete array is:", pos) 
                                                    #Displays result

  def Sum_Negative(self):
  #Sum of all Negative numbers in the list
    neg = sum(x for x in m if x < 0) #Sum numbers that are less than zero
    if neg is 0:
       print("There are no negative numbers in the complete array") #To display 
                                        #if there are no negative numbers
    else:
      print("The sum of all negative number in the complete array is:", neg) 
                                                        #Displays result

#Creating an object for the class Allarrays
ca = Allarrays()

#Calling the functions through the class
ca.Check_Sort()
ca.Sorting_array(arr)
ca.Not_Sorted()
ca.missing_element()
ca.Concatenate_arrays()
ca.Rvsal(m)
ca.nth_highest()
ca.Sum_Positive()
ca.Sum_Negative()

In the entire class, the Sorting_array function was recalled thrice after being defined and it keeps throwing the NameError please help. Thank you!

CodePudding user response:

Since you are calling a function from the same class, you have to use self.Sorting_array() when you call it.

By the way, according to PEP8 standard, you should try to name your functions lowercase.

  •  Tags:  
  • Related