2

I am trying to set up a function that records to 2 variables and a dictionary, the dictionar works but the two variables return the wrong things

mydict{}
fname = 0
lname = 0

def enterdetails(fname, lname):
    fname = input ("First Name: ");
    fnamedict = fname
    mydict['FirstName'] = fnamedict;

   lname = input ("Last Name: ");
   lnamedict = lname
   mydict['LastName'] = lnamedict;
   print(fname)
   print(lname)

   return mydict
   return (fname, lname)

fname, lname = enterdetails(fname, lname)
print(fname, lname)
print(mydict)

However the variables of fname and lname come out as FirstName and LastName respectively. How would I fix this?

9
  • 1
    You can't return multiple variables. A solution would be putting them all in one dict. Commented Oct 8, 2014 at 7:52
  • 2
    Your code does not make sense, you have 2 return statements, only the first one will execute. You can return a tuple of your dict and variables like so return mydict, fname, lname Commented Oct 8, 2014 at 7:52
  • You could look into using a yield statement instead though. Commented Oct 8, 2014 at 7:53
  • 2
    @VincentBeltman - you can absolutely return multiple variables - return them in a tuple - just as the OP is doing in the line return (fname, lname)- although the brackets aren't needed in this case. Commented Oct 8, 2014 at 7:53
  • @VincentBeltman: not true. Commented Oct 8, 2014 at 7:54

3 Answers 3

4

You have two return statements, but only the first is returned. Instead, return all three variables together as a tuple:

def enterdetails(fname, lname):
    ...
    return mydict, fname, lname

mydict, fname, lname = enterdetails(fname, lname)
print(fname, lname)
print(mydict)
Sign up to request clarification or add additional context in comments.

Comments

1

The dictionary works because you have it set as a global variable. However, the function is actually returning the "dictionary" first, so your unpacking is all messed up.

Remove the return mydict or use return mydict, (fname, lname), so you will end up with:

mydict, (fname, lname) = enterdetails(fname, lname)

But as I mentioned above, mydict is a global variable, so it is unnecessary to return the value.

1 Comment

I had to read this twice to understand it, but that is a very good point about global variables that the other answers didn't address.
0

You can't put a return under a return like

return mydict
return (fname, lname) #this won't return any thing

One of the ways that you could do is:

  ....
  return [mydict, fname, lname]

data = enterdetails(fname, lname)
mydict = data[0]
fname = data[1]
lname = data[2]

4 Comments

you don't need to use a list to return multiple items - you can just use return mydict, fname, lname and then mydict, fname, lname = enterdetails(fname,lname)when you call it.
@TonySuffolk66 yeah just mentioned one of the ways, edit it to make it better
Another answer has been posted using tuple - no point changing your answer to copy someone elses.
@TonySuffolk66 didn't see the other answer so left mine as it was previously

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.