On 25Dec2015 15:05, princeud...@gmail.com <princeud...@gmail.com> wrote:
i have gotten the answer of that problem

Please include some context when posting to the list; there are many discussions and it is best to include a little more than the subject line in such things. It is also polite to post your working solution for the benefit of those who have assisted you, or for others with a similar problem.

Then two more messages in quick succession: this list is not an instant messaging system. Please try to post a single message, with your question and also what code you have already.

Regarding your actual question, commentry below the quoted text follows...

On 25Dec2015 15:08, princeud...@gmail.com <princeud...@gmail.com> wrote:
Create a function manipulate_data that does the following
Accepts as the first parameter a string specifying the data structure to be used "list", 
"set" or "dictionary"
Accepts as the second parameter the data to be manipulated based on the data 
structure specified e.g [1, 4, 9, 16, 25] for a list data structure
Based off the first parameter

   return the reverse of a list or
   add items `"ANDELA"`, `"TIA"` and `"AFRICA"` to the set and return the 
resulting set
   return the keys of a dictionary.

#my solution is:
def manipulate_data(dic,dict_data = {'name':'prince','age':21,'sex':'male'}):
 return dict_data.keys()

def manipulate_data( alist, list_data = [2,8,16,23,14]):
 return list_data.reverse()

def manipulate_data(aset, set_data = {"bee","cee","dee"}):
 set_data = {"bee","cee","dee"}
 set_data.update("ANDELA","TIA","AFRICA")
 return dictionary_data
#please what is wrong with my code

The most obvious thing that is wrong it that you have 3 functions here with the same name. In Python that means: define the first function and bind it to the name "manipulate_data". Then define the second and bind it to the same name as the first, discarding the first function. The define the third and bind it to the same name again, discarding the second function.

Your task is asking for a _single_ function which behaves differently according to a parameter which tells it what kind of data is has been given, so you want a function which starts like this:

 def manipulate_data(kind, data):

and you are supposed to pass in the string "list", "set" or dictionary for the first parameter, and some corresponding list, set or dictionary as the second parameter. There should _not_ be default values for either of these.

Instead, your function should examine the "kind" parameter and decide what to do. So it would reasonably look like this (untested):

 def manipulate_data(kind, data):
   if kind == 'list':
     ... do stuff with data using it as a list ...
   elif kind == 'set':
     ... do stuff with data using it as a set ...
   if kind == 'dictionary':
     ... do stuff with data using it as a dictionary ...
   else:
     raise ValueError("invalid kind %r, expected 'list', 'set' or 'dictionary'" 
% (kind,))

Try starting with that and see how you go.

Cheers,
Cameron Simpson <c...@zip.com.au>
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to