Сохранение информации в словарь Pyhton
Не могу разобраться как сохранять информацию о более чем двух пользователей. Выводит только последнего введенного пользователя. Буду очень благодарен, так как инфы не нашел.
user_name = {}
arhive = True
while arhive:
user_names = input("Pleas creat a nickname: ")
location = input("Where are you from: ")
pin = input("Create a pin: ")
user_name['Name'] = user_names
user_name['Location'] = location
user_name['Pin'] = pin
finis = input("Want to register one more user?(yes/no)?: ")
if 'yes' in finis:
continue
if 'no' in finis:
print ("You have successfully register.")
arhive = False
for key,value in user_name.items():
print (f"{key}: {value}")
Ответы (1 шт):
Автор решения: Zhihar
→ Ссылка
вариант 1:
если нужен словарь - так и используйте словарь где в качестве ключа - имя
user_name = dict()
arhive = True
while arhive:
user_names = input("Pleas creat a nickname: ")
location = input("Where are you from: ")
pin = input("Create a pin: ")
user_name[user_names] = {'location': location, 'pin': pin}
finis = input("Want to register one more user?(yes/no)?: ")
if 'no' in finis:
print ("You have successfully register.")
arhive = False
for key, value in user_name.items():
print (f"{key}: {value}")
вариант 2:
или используйте список, где в качестве элемента будет словарь:
user_name = list()
arhive = True
while arhive:
user_names = input("Pleas creat a nickname: ")
location = input("Where are you from: ")
pin = input("Create a pin: ")
user_name.append({'Name': user_names, 'Location': location, 'Pin': pin})
finis = input("Want to register one more user?(yes/no)?: ")
if 'no' in finis:
print ("You have successfully register.")
arhive = False
for obj in user_name:
print (f"{obj[name]}: {obj[location]}, {obj[pin]}")
P.S.
а вы принципиально у слов последние буквы отрезаете?