Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python3字典-替换内容问题_Python 3.x - Fatal编程技术网

Python 3.x Python3字典-替换内容问题

Python 3.x Python3字典-替换内容问题,python-3.x,Python 3.x,事实上,我自己也在为project工作,只是为了学习,我知道这个项目很弱,因为任何看到该文件的人都可以看到源文件,而且它没有加密,但我怀疑: 这是我的代码: defaultusername = "Alex" defaultpassword = "asd123" inputusername = input("Introduce tu username: ") inputpassword = input("Introduce la contraseña: ") credenciales = {

事实上,我自己也在为project工作,只是为了学习,我知道这个项目很弱,因为任何看到该文件的人都可以看到源文件,而且它没有加密,但我怀疑:

这是我的代码:

defaultusername = "Alex"
defaultpassword = "asd123"


inputusername = input("Introduce tu username: ")
inputpassword = input("Introduce la contraseña: ")

credenciales = {'ID': 1, 'plataforma' : 'steam', 'username' : 'asd', 'password' : 'password',
                'ID': 2, 'plataforma' : 'steam2', 'username2' : 'asd2', 'password' : 'password2'}


if inputusername == defaultusername and inputpassword == defaultpassword:
    opcion = int(input('''
1) Consultar mis credenciales
2) Añadir credenciales (u/e:p)
3) Borrar credenciales

Selecciona una opción: '''))

    if opcion == 1:
        print("Credenciales:")
        for i in range(credenciales['ID']):
            print(credenciales['plataforma'],">",credenciales['username'].strip() + ":".strip() + credenciales['password'])
    elif opcion == 2:
        pass
    elif opcion == 3:
        pass
    else:
        print("Selecciona una opción correcta")


else:
    print("Datos incorrectos")
结果只打印字典的第二行(非常确定在字典上添加额外一行后内容会被替换,但我不确定如何修复此问题,非常感谢任何帮助!)

结果:

Selecciona una opción: 1
Credenciales:
steam2 > asd:password2
steam2 > asd:password2

python字典中的键必须是唯一的。在您的字典中,您已经定义了键
ID
platforma
password
在dict中定义了两次,因为dict键必须是唯一的,因此最终会覆盖这些键的值

credenciales={'ID':1,'plataforma':'steam','username','asd','password','password',
'ID':2,'plataforma':'stream2','username2':'asd2','password':'password2'}
印刷品(credenciales)
输出

{'ID': 2, 'plataforma': 'steam2', 'username': 'asd', 'password': 'password2', 'username2': 'asd2'}

因此,您可能需要重新考虑如何存储这些信息的数据结构。可能是一个dict列表,也可能是一个基于id号的dict列表。

字典中的键必须是唯一的。这里您已经写了两次密钥
ID
platforma
密码。所以ID的值是2,然后在创建字典后,循环并打印这两行,添加到这一行,您将看到
print(credenciales)