Python Forum

Full Version: TypeError: 'module' object is not callable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I getting following error on the attached file. please help...
Line 21 should be:
i=random.randint(1, 10)
Output:
Jumbled Word is--- oeums Please, Enter your answer mouse syam congratulations
[quote="BashBedlam" pid='153674' dateline='1645323590']
Line 21 should be:
i=random.randint(1, 10)
Thank you very much dear.... Heart
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.
Thank you so much for your support and guidance.... Dance