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
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)
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)
912 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
610 GDB gets version error when attempting to debug with the Presense SDK (IDE)
617 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.