def build_profile(first, last, **user_info):
"""Build a dictionary containing everything we know about user."""
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key, value in user_info.items():
profile[key] = value
return profile
user_profile = build_profile('albert', 'einstein',
location='princeton',
field='physics')
print(user_profile)
output :
{'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'}
Iâve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the âpreformatted textâ tool in the editor (</>) to add backticks around text.
first of all I am also a absolute beginner in terms of programming and python, but I tried to solve the task and made my own attempt at it.
Here are my thoughts:
For creating a dictonary, i would want to have a unique key that stores all the information of the person in the value as a list, so I created a code aiming for that.
I prompt the user for data, because i dont have a file or anything else as input.
profile = {}
count = 0
while True:
newprofile = input("Want to create new profile? (y/n): ")
if newprofile == "y":
lastname = input("Enter lastname: ")
surname = input("Enter surname: ")
location = input("Enter location: ")
field = input("enter field: ")
user = lastname[:2] + surname[:2] + str(count) #could blow up, it's just a quick solution
userinfo = [lastname,surname,location,field]
profile[user] = userinfo
count = count + 1
else:
break
for key,val in profile.items():
print(val)