I need help with the Python list output.

Asked 1 years ago, Updated 1 years ago, 92 views

I was happy that the answer came out earlier, but I came back to the starting point and posted the question again... The question now is, is it an address book in which you save the information of 3 friends and then input the information? It's about making the same thing. When I first made it,

friends = {}

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

print(friends)

inform = input ('Enter information:')
if inform in friends.keys() :
    print( friends[inform])
else :
    print('Information missing.')

I made it in this format, but at this time, I thought that only the friend's information that I wrote last came out, not the information of 3 people, so I only needed to modify the friends above.

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)


inform = input ('Enter information:')
if inform in friends.keys() :
    print( friends[inform])
else :
    print('Information missing.')

After that, I thought it was completed by getting an answer and revising it, but I would appreciate it if you could tell me what the problem is because keys are only available in the dictionary.

python dictionary list save

2022-09-22 13:30

1 Answers

Friends is a list of dictionaries. Cannot write keys() function because it is a list.

Why don't you use the for statement to approach the elements in the list one by one?


2022-09-22 13:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.