I'm curious about how to save Python Dictionary

Asked 2 years ago, Updated 2 years ago, 131 views

friends = {}

for i in range (0,3,1) :
    friends = {'name': input('name:')', 'tel':input('phone number:')', 'addr':input('address:')', 'age':input('age:')}

print(friends)

I'm saving the addresses of three friends If you follow the code now and run it last, only the information of the last friend you entered will be listed. What should I do to have all three listed?

python dictionary save

2022-09-22 19:27

2 Answers

A dictionary is a structure of data stored in the form of key:value.

That is, one dictionary can represent one record.

To store multiple records, you can use a data structure called a list.

friends = []

for i in range (0,3,1) :
    friends.append({'name': input('name:'), 'tel':input('phone number:'), 'addr':input('address:'), 'age':input('age:')})

print(friends)


2022-09-22 19:27

friends = []

for i in range (0,3) :
    name = input ('Enter a name:')
    tel = input ('Enter phone number :')
    addr = input ('Enter address:')
    age = input ('Enter your age:')
    friends.append({'name': name, 'tel': tel, 'addr': addr, 'age':age})

print(friends)


2022-09-22 19:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.