0

Python is new to me. I'm on the chapter of Function now.

Here's my code - Just concerned about why I am unable to use the input feature in this case:

def build_profile (first_name, last_name, **personal_traits):
    profile = {}
    profile ['First name'] = first_name
    profile ['Last name'] = last_name
    for key,value in personal_traits.items():
        profile[key]=value
    return profile

fn = input("What's your first name? ").title()
ln = input("What's your last name? ").title()
ptr = input('What are your personal traits? name three ').title()

your_info = build_profile(fn,ln,ptr)
print(your_info) 

Error

What's your first name? liwei
What's your last name? hsu
What are your personal traits? name three x,y,z
Traceback (most recent call last):
  File "/Users/liwei/PycharmProjects/pythonProject/ReviewLession/For Whatever Practice Use.py", line 14, in <module>
    your_info = build_profile(fn,ln,ptr)
TypeError: build_profile() takes 2 positional arguments but 3 were given

I've tried It worked perfectly fine by taking away the input section, which I understood.

I tried adding ** onto this part:

your_info = build_profile(fn,ln,**ptr)
print(your_info)
  • but it didn't work! Not entirely sure how to solve this if insisted on using input function.
  • What are your sample inputs? – stuck Aug 18 '22 at 14:33
  • 1
    always put FULL error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information in the full error/traceback. – furas Aug 18 '22 at 14:33
  • 3
    `**personal_traits` means that the rest of the arguments are keyword arguments, of the form `name=value`. `personal_traits` will contain a dictionary of those arguments. – Barmar Aug 18 '22 at 14:33
  • You have a function that takes two positional arguments, and you gave it three. The fact that there are some named-only arguments after that is entirely irrelevant. – Silvio Mayolo Aug 18 '22 at 14:34
  • 2
    What do you *expect* the `**` to do here? You'd need to pass key-value pairs in some way (named arguments), but you're only getting one plain value from `input`, so it's not obvious how that's supposed to work. – deceze Aug 18 '22 at 14:36
  • you would have to create dictionary like `ptr = {"first"; "something", "second": "something", "third": "something"}` to send it as `**ptr` - and then `build_profile()` would thread it as named arguments `first="something", second="something", third="something"` – furas Aug 18 '22 at 14:36
  • And in your code, you seem to expect it to be key-value pairs. But the user just entered a single string, and all you did was add capital letters to it. What are they supposed to enter in response to the personal traits prompt? – Barmar Aug 18 '22 at 14:36
  • Try `build_profile(fn,ln, **{f"trait{i}": trait for i, trait in enumerate(ptr.split())})`. – Samwise Aug 18 '22 at 14:37
  • maybe it would be simpler to send it as single variable with dictionary - without `**` - and later assign `profile = personal_traits` – furas Aug 18 '22 at 14:40

0 Answers0