2014-04-04 49 views
0
if 0 in dict.values(feedbackdict["%s" %log]): 
    print ("Sorry, you have either not been left feedback for your spelling tesy yet or have not yet completed your spelling test. \nYou will be returned to the main menu.") 
    LoggedInStudent(log) 
else: 
    feedback = dict.values(feedbackdict["%s" %log]) 
    print (feedback) 

所以我想要做的是确保如果用户没有收到任何反馈(这意味着键的值为'0',程序会识别这个并将用户返回到主菜单,但是如果反馈不是'0',那么程序应该认识到那里有一个字符串并将其定义为'feedback',然后显示它以向用户显示他们的反馈描述符'值'需要一个'字典'对象,但收到'int'/'str'python

我想将'feedback'定义为某个键的值(在这种情况下键是%log,可能是用户的名字,例如'亚光'),但是当我尝试执行此操作时,我收到错误:

TypeError: descriptor 'values' requires a 'dict' object but received a 'int' 

我很困惑,为什么这不起作用。 “反馈”不应该简单地定义为链接到密钥的价值?举例来说,在我的字典里,键“Matt1”的值是“干得好!”,但是当程序试图收集这些它给我的错误:

TypeError: descriptor 'values' requires a 'dict' object but received a 'str' 

我很困惑,为什么程序需要一个字典对象。有什么办法可以解决这个问题吗?对不起,如果解释有点低于标准杆。

回答

1

这很简单。从我注意到的情况来看,我认为你正在错误地使用你的字典对象。为了澄清...

如果您dict变量命名为feedbackdict,你为什么要访问它的值dict.values(feedbackdict[key])时,你应该简单地访问它feedbackdict[key]

你得到TypeError异常,因为dict.values是dict类的不受约束的方法,并采取了dict实例作为它的唯一参数(您通过它的int一次,一个str其他)

试着这么做这不是..

feedback = feedbackdict["%s" % log] 
if feedback == 0: 
    print("Sorry, you have either not been left feedback for your spelling test yet or have not yet completed your spelling test. \nYou will be returned to the main menu.") 
    LoggedInStudent(log) 
else: 
    print(feedback) 

希望这有助于

+0

非常感谢,这工作:) – user3112327

相关问题