KhornetheGrim
KhornetheGrim

Reputation: 13

Python Convert set to list, keep getting TypeError: 'list' object is not callable

I am trying to write a program that adds items to a list based on a random number. Part of the program is potentially rolling additional items, but all duplicate items should be rerolled. My issue is that when I try to do this using the methods I was able to find (compare list to set of list to test for dups, then save the set to the list), but I keep getting TypeError: 'list' object is not callable. The confusing thing is that when I test this with simple lists I set up, it works fine.

This is my code

from random import randint

# Dice roller function
def roll_dice(dice, sides, bonus):
    count = 0
    roll = 0
    for count in range(dice):
        roll += randint(1, sides)
    roll += bonus
    return roll

list = ['1', '2', '3', '4', '5']
print_list = ''
for x in range(0, len(list)-1):
    print_list += ' ' + list[x]
print print_list[1:]

# Minor special armor ability roller
def armor_special_ability():
    reroll = 1
    ability = []
    abil_list = ''
    while reroll > 0:
        result = int(raw_input('Roll:'))#roll_dice(1,100,0)
        if result <= 25:
            ability.append('Glamered')
        elif result <= 32:
            ability.append('Light Fortification')
        elif result <= 52:
            ability.append('Slick')
        elif result <= 72:
            ability.append('Shadow')
        elif result <= 92:
            ability.append('Silent Moves')
        elif result <= 96:
            ability.append('Spell Resistance (13)')
        elif result <= 97:
            ability.append('Improved Slick')
        elif result <= 98:
            ability.append('Improved Shadow')
        elif result <= 99:
            ability.append('Improved Silent Moves')
        elif result <= 100:
            reroll += 2
        reroll -= 1
        if len(ability) > len(set(ability)):
            reroll += (len(ability) - len(set(ability)))
            ability = list(set(ability))
    return ability

print armor_special_ability()

Can anyone help me figure out why I keep getting this error? I've spent hours searching the net with no success.

Upvotes: 1

Views: 6522

Answers (2)

NetrobeWebby
NetrobeWebby

Reputation: 116

The problem can mistakenly happen if you have previously defined a variable with a built-in function name.

The built-in functions, sorted and print, are available by default. When a new variable or function or class is created using that exact same name with any other built-in, it's going to override that built-in from that point in code.

Take for example the code below

>>> my_list = ["banana", "cherry", "apple"]
>>> new_list = sorted(my_list)
>>> print(new_list)
['apple', 'banana', 'cherry']
>>> sorted = None
>>> new_list = sorted(my_list)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

The NoneType error happened, cause the built-in function is now a NoneType variable.

Don't use built-in function names when naming new variables

Upvotes: 0

Anand S Kumar
Anand S Kumar

Reputation: 90889

The issue is in line -

list = ['1', '2', '3', '4', '5']

You are overwriting the in-built list function with your list , after this assignment , if you try to call list() it would cause error, as it would try to access the list you defined and call it.

Use a different name, do not ever use list as a name for a variable (unless you really wanted to overwrite the list in-built function) , since it overwrites inbuilt functions.

Upvotes: 6

Related Questions