0

TypeError:无法将“NoneType”对象隐式转换为 str。这是我尝试使用函数将条目硬编码到字典中时遇到的错误。让用户输入有效,并将其放入字典中,但这不起作用。我已经搜索了诸如此类的其他错误(意思是 TypeError。),但我什么也没找到。有此错误的其他两个(Thisthis)条目与我无关。

所以。我试图让 AweInspiring 和 BeingAwesome 成为一个打印函数,认为它会正确打印到成就字典中,因为简单地将 AweInspiring 和 BeingAwesome 放在那里会导致它说需要定义它。然后它出现了这个错误。我认为它应该工作,但它没有,我不知道为什么。有谁能够帮我?

    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()
4

2 回答 2

1

print()不返回任何东西(默认情况下,它返回None)。那么当你这样做时achievements[achievename] = achievedesc,python实际上是在做这个:

{None:None}

然后你在做:

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

x关键在哪里Noneachievements[x]价值在哪里(也是None

但是你不能连接一个字符串和一个 NoneType (因此错误)。


非常简单,您的代码以最简单的形式(例如),您正在尝试这样做:

print('Hello' + None)

要解决这个问题,您可以制作AweInspiringBeingAwesome清空字符串:

AweInspiring = ''
BeingAwesome = ''
于 2013-07-10T10:18:02.677 回答
0

在我的空闲中对其进行了编辑,添加了一项成就,并最终为自己感到自豪,因为它对我来说很好用:

 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()

我的对话:

=================================重新开始================= ================

  >>> 
 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'}
于 2013-07-10T10:26:16.543 回答