简体   繁体   English

字典写作和硬编码条目

[英]Dictionary Writing and Hard-Coded Entries

TypeError: Can't convert 'NoneType' object to str implicitly. TypeError:无法将'NoneType'对象隐式转换为str。 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. 我已经搜索了其他错误,例如this(意味着TypeError。),但是我什么也没想出来。 The other two( This and this ) entries that had this error were irrelevant to me. 其他两个具有此错误的条目( Thisthis )与我无关。

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. 我试图使AweInspiring和BeingAwesome成为一个打印函数,认为它可以正确地打印到Achievements字典中,因为仅将AweInspiring和BeingAwesome放在那会导致它说需要定义。 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()

print() does not return anything (by default, it returns None ). print()不返回任何内容(默认情况下,它返回None )。 So then when you do achievements[achievename] = achievedesc , python is actually making this: 因此,当您完成achievements[achievename] = achievedesc ,python实际上就是这样做的:

{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 ) 其中x是键Noneachievements[x]是值(也是None

But you can't concatenate a string and a NoneType (hence the error). 但是您不能将字符串和NoneType连接起来(因此出现错误)。


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: 为了解决这个问题,您可以将AweInspiringBeingAwesome空字符串:

AweInspiring = ''
BeingAwesome = ''

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'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM