Reputation: 415
def build_profile(f_name,l_name,**qualification):
profile={}
profile['first']=f_name
profile['last']=l_name
for k,v in qualification.items():
profile[k]=v
return profile
pl=[]
while True:
pl.append(build_profile(input("Enter first name: "),\
input("Enter last name:"),graduation=input("Enter Subject:"),\
masters=input("Enter Subject:")))
if input("Want to finish?(Y/N) ").upper()=="Y":
break
print(pl)
The given function accepts user input values for 'graduation' and 'masters' key but I was thinking that for different users level of education might be different some might be PHD and some might not have even completed High school. I tried to convert the key values for the dictionary 'qualification' to be input by user but whenever i convert
graduation=input("Enter Subject:")
to
input("Enter value:")=input("Enter Subject:")
I get an error "Keyword can't be an expression". On searching internet i knew about split() function in python can accomplish it but I was not able to make it work in my code.
#SOLVED Based on answers I modified my code as below and now its working as i desired:
def build_profile(f_name,l_name,qualification):
profile={}
profile['first']=f_name
profile['last']=l_name
for k,v in qualification.items():
profile[k]=v
return profile
pl=[]
while True:
first_name=input("first name: ")
last_name=input("last name: ")
no_of_qual=int(input("How many qualification you want to add?"))
dict_qual={}
for qual in range(no_of_qual):
dict_qual[input("Enter subject")]=input("Enter qualification")
inv_qual = {v: k for k, v in dict_qual.items()}
pl.append(build_profile(first_name,last_name,inv_qual))
if input("Want to add more users?(Y/N) ").upper()=="N":
break
print(pl)
Upvotes: 1
Views: 65
Reputation: 520
You're trying to use a string as an argument name which is not possible. It would probably be better to input an actual dictionary than use an ** argument:
def build_profile(f_name: str, l_name: str, qualification: dict) -> dict:
return {'first': f_name, 'last': l_name, **qualification}
pl=[]
while True:
pl.append(build_profile(input("Enter first name: "), \
input("Enter last name: "), \
{input("Enter value: "):input("Enter subject: ")}))
if input("Want to finish? (Y/N) ").upper() == "Y":
break
print(pl)
It asks for a value, then a subject (only once).
Upvotes: 2
Reputation: 382
what you probably want is not to have different keys for different people, but to add one key, for instance "level", for which you can use the different values you ask for (like PHD). So then it looks like this:
def build_profile(f_name,l_name,**qualification):
profile={}
profile['first']=f_name
profile['last']=l_name
for k,v in qualification.items():
profile[k]=v
return profile
pl=[]
while True:
pl.append(build_profile(input("Enter first name: "),\
input("Enter last name:"),graduation=input("Enter Subject:"),\
masters=input("Enter Subject:"),\
level=input("Enter the level"))
if input("Want to finish?(Y/N) ").upper()=="Y":
break
print(pl)
Upvotes: 1