0

TypeError: Can't convert 'NoneType' object to str implicitly. That is the error I get when I try to hard-code an entry into a dictionary by using a function. Having user input works, and puts it into the dictionary, but this won't work. I've searched for other errors such as this(meaning the TypeError.), but I have come up with nothing. The other two(This and this) entries that had this error were irrelevant to me.

So. I tried to make AweInspiring and BeingAwesome a print function thinking it would print properly into the Achievements dictionary because simply putting AweInspiring and BeingAwesome there would lead to it saying it needs to be defined. Then it turned up this error. I think it should work, but it doesn't and I don't know why. Can anybody help me?

    achievements = {}
    AweInspiring = print()
    BeingAwesome = print()

    def dovar():
        global achievements
        print('Type in either \'achievement get\' or \'view achievements.\'')
        achieve = input()
        if achieve == 'view achievements':
            print('Achievements')
            for x in achievements.keys():
                print('Achievement Name: ' + x + '\tFor: ' + achievements[x])
        elif achieve == 'achievement get':
            achieveget()
        elif achieve == 'achieve':
            hardachieve()

    def achieveget():
        print('Add Achievement')
        achievename = input('Name of achievement earned: ')
        achievedesc = input('How was the achievement earned: ')
        achievements[achievename] = achievedesc
        dovarloop()

    def hardachieve():
        achievename = AweInspiring
        achievedesc = BeingAwesome
        achievements[achievename] = achievedesc
        dovar()

    def dovarloop():
        dovar()

    dovar()
Community
  • 1
  • 1
Justin
  • 283
  • 2
  • 5
  • 11

2 Answers2

1

print() does not return anything (by default, it returns None). So then when you do achievements[achievename] = achievedesc, python is actually making this:

{None:None}

Then you're doing:

print('Achievement Name: ' + x + '\tFor: ' + achievements[x])

Where x is the key None and achievements[x] is the value (which is also None)

But you can't concatenate a string and a NoneType (hence the error).


So pretty much, your code in simplest form (as an example), you're trying to do this:

print('Hello' + None)

To solve this, you can make AweInspiring and BeingAwesome empty strings:

AweInspiring = ''
BeingAwesome = ''
TerryA
  • 58,805
  • 11
  • 114
  • 143
  • Then how would I print "AweInspiring" and "BeingAwesome?" What would I have to define it as? – Justin Jul 10 '13 at 10:20
  • @Justin Well I'm not sure, that's for you to decide. If you want it as nothing, you can use an empty string `''` – TerryA Jul 10 '13 at 10:21
  • Facepalm* I cannot believe I did not put '' first... that's what I'd do for an if statement or anything... D: – Justin Jul 10 '13 at 10:24
  • I'll accept this answer because it has the answer in the comments xD – Justin Jul 10 '13 at 10:25
0

Edited it in my Idle, added an achievements, and ended up being proud of myself, because it works fine for me:

 achievements = {}

def dovar():
    global achievements
    print('Type in either \'achievement get\' or \'view achievements.\'')
    achieve = raw_input()
    if achieve == 'view achievements':
        print('Achievements')
        for x in achievements.keys():
            print('Achievement Name: ' + x + '\tFor: ' + achievements[x])
   elif achieve == 'achievement get':
       achieveget()
   elif achieve == 'achieve':
       hardachieve()

def achieveget():
    print('Add Achievement')
    achievename = raw_input('Name of achievement earned: ')
    achievedesc = raw_input('How was the achievement earned: ')
    achievements[achievename] = achievedesc
    dovarloop()

 def hardachieve():
    global achievments
    achievename = "got a cat"
    achievedesc = "found one"
    achievements[achievename] = achievedesc
    #dovar()

def dovarloop():
    dovar()

dovar()

My conversation:

================================ RESTART ================================

  >>> 
 Type in either 'achievement get' or 'view achievements.'
 achievement get
 Add Achievement
 Name of achievement earned: got a cat
 How was the achievement earned: found one
 Type in either 'achievement get' or 'view achievements.'
 view achievements
 Achievements
 Achievement Name: got a cat    For: found one
 >>> 
 >>> ================================ RESTART ================================

 >>> hardachieve()

 >>> achievements
 {'got a cat': 'found one'}
Pawel Miech
  • 7,742
  • 4
  • 36
  • 57