繁体   English   中英

描述符“值”需要一个“ dict”对象,但接收到一个“ int” /“ str” Python

[英]descriptor 'values' requires a 'dict' object but received a 'int'/'str' python

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”,然后将其显示给用户以显示他们的反馈。

我试图将“ feedback”定义为某个键的值(在这种情况下,键为%log,它可以是用户名,例如“ matt”),但是当我尝试执行此操作时,我收到了错误消息:

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

我对为什么这不起作用感到困惑。 不应仅将“反馈”定义为链接到键的值? 例如,在我的字典中,键“ Matt1”的值为“ Good job!”,但是当程序尝试收集它时,它给我错误:

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

我对为什么程序需要dict对象感到困惑。 有什么办法解决这个问题? 很抱歉,如果解释有点不合标准。

很简单 从我注意到的情况来看,我认为您使用字典对象的方式有误。 澄清...

如果您的dict变量名为feedbackdict ,那么当您只应将其作为feedbackdict[key]访问时,为什么要以dict.values(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)

希望这可以帮助

暂无
暂无

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

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