Python Forum
TypeError: 'module' object is not callable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: 'module' object is not callable
#1
I getting following error on the attached file. please help...

Attached Files

.py   permutation JumbledWord.py (Size: 2.34 KB / Downloads: 280)
Reply
#2
Line 21 should be:
i=random.randint(1, 10)
Output:
Jumbled Word is--- oeums Please, Enter your answer mouse syam congratulations
Reply
#3
[quote="BashBedlam" pid='153674' dateline='1645323590']
Line 21 should be:
i=random.randint(1, 10)
Thank you very much dear.... Heart
Reply
#4
import random
...
        i=round(10*random())
"import random" creates a module object for the random library and assigns the object to a variable named "random". When you call random(), you are treating it like a function. That is why you get a "module is not callable" error.

There are better ways to do this:
def choose(Dict):
    i=0
    print(len(Dict))
    while(1):
        i=round(10*random.random())
        print(i)
        if(i<=len(Dict)):
            break
    return(Dict[i])
As mentioned by BashBedlam you can use randint(). This eliminates the rounding and ensures that the random int is in range.
def choose(words):
    return words[random.randint(0, len(words)-1)
But why worry about rounding and range. random has other functions more suited for randomly selecting an item from a collection.You could use choice() to randomly picks an item for a list. You could use sample() like you did to jumble the letters in the word. My choice for randomizing words is "shuffle()". Shuffle randomizes the order of items in a collection, like shuffling cards in a deck. If you shuffle the words you can loop through the word list without worry of a word repeating.
player_points = {'syam':0, 'prabha':0, 'subramanyan':0}
words = ['janaki','pinku','pumpkin','tomato','television','mouse','computer','programming']
random.shuffle(words)

for word in words:
    jumble = "".join(random.sample(word, len(word)))
This is not a good way to store scores.
Plr1=('syam')
Plr2=('prabha')
Plr3=('subramanyan')
Ppt1=0
Ppt2=0
Ppt3=0
When you store information in individual variables Python cannot help you keep track of things. If Plr are player names and Ppt are player points, I would replace these 6 variables with 1 dictionary.
player_points = {'syam':0, 'prabha':0, 'subramanyan':0}
Now Python associates the player name with the player score. You don't have to worry about making an error incorrectly rewards the wrong player.

Your program currently uses "Turn" to keep track of the current player. Here, again, Python should do this job. If player_points is a dictionary, dictionaries are iterable. This means you can loop through the dictionary keys (player names) in a for loop.
    for player in player_points:
        print(player)
        print("Jumbled Word is---")
        print(jumbled_word)
        if input("\tPlease, Enter your answer\t") == word:
            player_points[player] += 1
            print(player, "congratulations\n\n")
        else:
            print("Better Luck next time\n Currect Answer is", word)
Not only does this make Python responsible for keeping track of the current player, it eliminates a lot of duplicate code. The code for player 1 is identical to the code for player 2 except for the player name and player score. By keeping player information in an iterable collection you can treat players generically. This reduces the code you have to write and maintain.
Reply
#5
Thank you so much for your support and guidance.... Dance
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 383 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 504 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 738 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 987 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,346 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  Need help with 'str' object is not callable error. Fare 4 826 Jul-23-2023, 02:25 PM
Last Post: Fare
  working with TLV module Object Jennifer_Jone 3 1,146 Mar-14-2023, 07:54 PM
Last Post: Jennifer_Jone
  TypeError: 'float' object is not callable #1 isdito2001 1 1,075 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 4,090 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  'SSHClient' object is not callable 3lnyn0 1 1,168 Dec-15-2022, 03:40 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020