0

I want to ask the user for 2 inputs, first name and last name, return a greeting and store the names in a Dictionary with the Keys being 'FName' and 'LName'

The following stores the greeting fine....

def name():
    d = {}
    x = input('Please enter your first name: ')
    y = input('Please enter your last name: ')
    d = {}
    d[x] = y

    print("Hello", x, y)
    print(d)

name()

but I am not sure how to get the key/values in the dictionary properly. Right now it stores the equivalent of:

{'Joe': 'Smith'}

I know I need to reformat the following line different I am just not sure how to approach it...

d[x] = y
1
  • Now you are storing the key first name with value last name. Commented Jan 2, 2018 at 23:22

3 Answers 3

4

You'll want to manually set the keys you are storing against

d['FName'] = x
d['LName'] = y

Or more simply

d = {
    'FName': x,
    'LName': y
}
Sign up to request clarification or add additional context in comments.

1 Comment

ahh I see now what he is trying to do
1

Here is another example:

def name():
    d = {}
    qs = dict(Fname='first name', Lname='last name')
    for k,v in qs.items():
        d[k] = input('Please enter your {}: '.format(v))
    return d

name()

3 Comments

Thanks that is a cool way to set it up. Dumb question, how can I print/see the dictionary results after it runs? I tried adding print(qs) and print(d). Neither of them worked...
@JD2775 those are local variables, you would need to return them. Try printing inside the function
ah, got it. Thanks again!
0

Nevermind, I figured it out. Sorry.

d['Fname'] = x
d['Lname'] = y

3 Comments

if this is truly your desired solution, you can either accept this answer or delete the question. Good job figuring it out, by the way :)
Thanks, how do you delete a question? I think I may have others going forward that I realize are easy right after I ask them :)
Hey that's good too, whatever works :) Feel free to keep them, or meta.stackexchange.com/a/42297/376870. Ah my mistake, you may not be able to delete it now "You can only delete it if you have non upvoted answers."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.