dogewang
dogewang

Reputation: 678

get TypeError when i import my own .py file

I am doing a sort program.i have two files called bubble(a bubble sort program) and cal_time(calculate the time),and they are in the same directory.

The problem is ,bubble work alone fluently. however,when i import bubble to my cal_time file and callback bubble sort,the interpreter show me the error message,and obviously there is no built_in function or method in my code:

Traceback (most recent call last):    
  File "F:/alogrithm/wzysort/cal_time.py", line 13, in <module>  
      bubble.bubble_sort(generate_random_list())  
  File "F:\alogrithm\wzysort\bubble.py", line 4, in bubble_sort  
       if a[indx] > a[indx+1]:  
    TypeError: unorderable types: builtin_function_or_method() > builtin_function_or_method()

cal_time.py:

import time
from wzysort import bubble
import random


def generate_random_list():
    result = []
    for i in range(10):
        result.append(random.random)
    return result

time_start = time.time()
bubble.bubble_sort(generate_random_list())
time_end = time.time()
print(time_end - time_start)

bubble.py:

def bubble_sort(a):
for i in range(len(a)-1):
    for indx in range(len(a[:-i-1])):
        if a[indx] > a[indx+1]:
            a[indx], a[indx + 1] = a[indx + 1], a[indx]

Upvotes: 2

Views: 42

Answers (2)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48090

In generate_random_list() function, you are doing random.random. Since it is a function, you should write it as random.random(). Hence, the code of your generate_random_list() function should be:

def generate_random_list():
    result = []
    for i in range(10):
        result.append(random.random())
    return result

Upvotes: 0

Rushy Panchal
Rushy Panchal

Reputation: 17542

Your issue lies here:

result.append(random.random)

You are appending the method random.random onto the list – which has the type builtin_function_or_method (thus resulting in the error you are receiving – how would you compare functions?).

Instead, you want to call the method:

result.append(random.random())

Upvotes: 2

Related Questions