Here is my program that takes a list a words and sorts them from longest to shortest and breaks ties with the random module.
import random
def sort_by_length1(words):
t = []
for word in words:
t.append((len(word), random(), word))
t.sort(reverse = True)
res = []
for length, randomm, word in t:
res.append(word)
return res
I get this error: TypeError: ‘module’ object is not callable
But when I do: from module import module It works?? Why is that?
randommodule has arandomfunction. When you doimport randomyou get the module, which obviously is not callable. When you dofrom random import randomyou get the function which is callable. That’s why you don’t see the error in the second case.You can test this in your REPL.