python-3.x - 类型错误 : 'type' object does not support item assignment

标签 python-3.x

尝试向 Python 3 的字典中添加一些内容(例如创建用户名和密码)。但是我遇到一个错误,告诉我该类型不支持项目分配。我是编程新手,所以请耐心等待。我只是假设在 python 中的字典中添加键和值不允许使用输入变量。

userlist = dict


def main():
    add_user()
    print(userlist)


def add_user():
    global new_user
    global new_password
    print('Please indicate your desired username')
    new_user = str(input('User: ')).lower
    print('Please indicate your desired password')
    new_password = str(input('Password: '))
    print('Please re-enter your password')
    password_addcheck = str(input('Password:'))
    if password_addcheck == new_password:
        print('Thank you, you are now successfully registered')
        userlist[new_user] = new_password
    else:
        print('Password does not match, please repeat process')
        add_user()


main()

最佳答案

@loocid 是对的,但你的代码的第一行是错误的:

userlist = dict

应该是

userlist = dict()

userlist = dict 将 python 关键字(和数据类型)dict 分配给变量 userlist - 当您尝试使用定义 时,您看到的错误实际上会出现dict 就好像它是一本字典,而不是用于创建字典的关键字。

关于python-3.x - 类型错误 : 'type' object does not support item assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56018370/

相关文章:

python - 雅虎天气 API 2019 - 类型错误/属性错误

python - 由 gunicorn 运行的 Flask 应用程序在一段时间后被挂起

python - 处理 makefile 对 python 数据分析的作用的最先进方法是什么?

python - 无法使用python scrapy框架在postgre中插入日期

python - 将文本元素保存到Python中的字典中

python - 使用 list.count 搜索列表中的多个项目?

python - <class 'UnicodeDecodeError' > 仅出现在 Python 3 中,而不出现在 Python 2 中

python - Plotly:如何调整 slider 和更新菜单的位置以为 x 轴刻度线腾出空间?

最适合构建双向市场的 Python 框架

python - 无法将标准输入重定向到 Python 脚本