0
      def myDict(key1, value1, key2, value2, value3, key3):
         # Write your code here
           a={key1:value1}
           print(a)
           a[key2]=value2
           print(a)
           a[key1]=value3
           print(a)
           a[key3]
           print(a)

      if __name__ == '__main__':
        key1 = 'name'
        value1 = 'rajesh'
        key2 = 'age'
        value2 = '21'
        value3 = 'vignesh'
        key3 = 'age'
        mydct = myDict(key1, value1, key2, value2, value3, key3)
        print(mydct if type(mydct) == dict else "Return a dictionary")
    

The driver function was pre-defined. The expected output is:

    {'name': 'rajesh'}
    {'name': 'rajesh', 'age': '21'}
    {'name': 'vignesh', 'age': '21'}
    {'name': 'vignesh'}

but i'm getting as :

    {'name': 'rajesh'}
    {'name': 'rajesh', 'age': '21'}
    {'name': 'vignesh', 'age': '21'}
    {'name': 'vignesh'}
    Return a dictionary

What mistake I'm doing?

1
  • 8
    myDict does not return a value. Make sure you understand what return does, and that print has nothing to do with it. Commented Sep 14, 2020 at 6:30

4 Answers 4

3

Add the following to the bottom of myDict:

    return a

Without it, myDict implicitly returns None, which is what you're seeing.

Sign up to request clarification or add additional context in comments.

Comments

2

You missed these two lines:

    del a[key3]
    return a

Here is the complete logic:

def myDict(key1, value1, key2, value2, value3, key3):
        a={key1:value1}
        print(a)
        a[key2]=value2
        print(a)
        a[key1]=value3
        print(a)
        del a[key3]
        return a

1 Comment

Welcome to Stack Overflow. Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it works better than what the OP provided. See How to Answer.
0

Just add return a at the last line of def function. I tried it and it got what you want. As said, if you don't put a return statement at the end of a function, you get a NoneType object, which is then it not recognize your mydct object as a dictionary, instead print the else statement, as you said beforehand above.

def myDict(key1, value1, key2, value2, value3, key3):
 # Write your code here
   a={key1:value1}
   print(a)
   a[key2]=value2
   print(a)
   a[key1]=value3
   print(a)
   a[key3]
   print(a)
   return a

if __name__ == '__main__':
    key1 = 'name'
    value1 = 'rajesh'
    key2 = 'age'
    value2 = '21'
    value3 = 'vignesh'
    key3 = 'age'
    mydct = myDict(key1, value1, key2, value2, value3, key3)
    print(mydct if type(mydct) == dict else "Return a dictionary")

Comments

-1

You what to change last statement to

Del a[key3]
Return a

It will give correct output.

Comments

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.