-1

I have a list, in the list I would like lists with the first item of the list containing a name and the second containing the dictionary that relates to that name for example:

[['James',{'name':'Thor', 'age':'30', 'race':'human', 'class':'fighter'}],['Tim',{'name':'Sally', 'age':'28', 'race':'half-orc', 'class':'cleric'}]]

Currently the code for my function is:

def add(chars):
    thisChar = []
    newChar = dict()
    print type(newChar)
    print type(chars)
    name = raw_input("What is your name? ").strip()
    thisChar.append(name)
    for field in attributes:
        userInput = raw_input("What is your " + field + "?")
        newChar[field] = userInput
    thisChar.append(newChar)
    chars.append(thisChar)
    return chars

But on the second pass though it throws a type error exception. What have I missed here?

My full code is:

attributes = ["Race", "Class", "Level", "Hit points", "Speed", "Initiative Bonus", "Strength", "Constitution", "Dexterity"
, "Inelegance", "Wisdom", "Charisma", "Armor class", "Fortitude", "Reflex", "Will Power", "Passive Insight", "Passive Perception"]


chars = []

def display(chars):
    print chars
    return

def add(chars):
    thisChar = []
    newChar = dict()
    name = raw_input("What is your name? ").strip()
    thisChar.append(name)
    for field in attributes:
        userInput = raw_input("What is your " + field + "?")
        newChar[field] = userInput
    thisChar.append(newChar)
    chars.append(thisChar)
    return chars

while True:
    print "Welcome to the D&D 4th edition Dungeon Master reference"
    print "D - Display "
    print "A - Add "
    print "Q - Quit"
    userChoice = raw_input("What would you like to do? ")
    userChoice.lower()
    if userChoice == "d":
        chars = display(chars)
    elif userChoice == "a":
        chars = add(chars)
    else:
        print "Bye"
        quit()

My full error is:

Traceback (most recent call last):
  File "C:/****/Python/charDisplay/AddAndShow.py", line 133, in <module>
    chars = add(chars)
  File "C:/****/Python/charDisplay/AddAndShow.py", line 120, in add
    chars.append(thisChar)
AttributeError: 'NoneType' object has no attribute 'append'

1 Answers1

1

The issue is most probably in this line -

chars = display(chars)

The function display() returns None , so when you are setting it back to chars , you are setting chars as None , so when you try to call add() again, with this chars , it gives you the error.

You do not need to set back the result of display() . You can do -

if userChoice == "d":
    display(chars)

Another issue , this line does not do anything -

userChoice.lower()

.lower() returns the stirng with lower case, it does not change the string in-place (it cannot, since strings are immutable), you have to assign back the returned value to userChoice . Example -

userChoice = userChoice.lower()
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176