描述符“values”需要“dict”对象,但接收到“int”/“str”python

2024-03-29 13:19:53 发布

您现在位置: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”定义为某个键的值(在本例中,键是%log,可以是用户名,例如“matt”),但当我尝试执行此操作时,收到错误:

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

我不明白这为什么不起作用。“反馈”不应该简单地定义为链接到键的值吗?例如,在我的字典中,键“Matt1”的值是“干得好!”,但是当程序试图收集此信息时,它会给我一个错误:

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

我不明白为什么程序需要dict对象。有什么办法解决这个问题吗?对不起,如果这个解释有点不合标准。


Tags: 字符串用户程序logyour定义havenot
1条回答
网友
1楼 · 发布于 2024-03-29 13:19:53

很简单。从我注意到的情况来看,我认为你使用dictionary对象的方式有点不对。澄清。。。

如果您的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)

希望这有帮助

相关问题 更多 >