0

I'm learning python, working with the sl4a to make a phone app. I'm having trouble with a dictionnary when passing it as a parameter. I've searched to see if someone had that problem already and found some alike questions but couldn't figure my mistake out.

Here's the the code (I removed all unrelevant parts):

def choiceForm(screenTitle, choices, posMessage, neutMessage,
               negMessage, questType, screen):

    qType = questType[screen]
    print(qType) #Not giving what it should
    # More code

def reservForm():
     questionType = {0 : 'single', 1 : 'date', 2 : 'input', 3 : 'input', 
                     4 : 'multi', 5 : 'single'}
     reservChoice, screenID, success = choiceForm(titlePack[screenID], 
         choicePack[screenID], 'Suivant', 'Précédent', 'Quitter',
          questionType, screenID)
     # More code

On execution, i get "s" for qtype which isn't what should be there. I want to access my dictionary in the called function to be able to get the value, given an index (screenID). I read a tutorial telling about unpacking the thing with a ** operator but i didn't understand a thing. Everything worked fine before i changed my parameter in choiceForm to pass the dictionary (i was just passing one of it's value 'til i realized i'd need the whole thing for a feature i've to add)

LionelM
  • 15
  • 5

1 Answers1

0

If you are talking about passing a Dictionary as a parameter, it should be as simple as passing any other parameters type and using this dictionary inside your calling function is as usual as you would work with regular dictionary:

dic.keys(), dic.values(), dic.items(), dic[key]=value...etc

Now, if you are talking about this notation: "**", this means passing parameters as dictionary, example from Python doc should make it clear:

def cheeseshop(kind, *arguments, **keywords):
    print "-- Do you have any", kind, "?"
    print "-- I'm sorry, we're all out of", kind
    for arg in arguments:
        print arg
    print "-" * 40
    keys = sorted(keywords.keys())
    for kw in keys:
        print kw, ":", keywords[kw]

Calling this function:

cheeseshop("Limburger", "It's very runny, sir.",
           "It's really very, VERY runny, sir.",
           shopkeeper='Michael Palin',
           client="John Cleese",
           sketch="Cheese Shop Sketch")

I suggest to you to read about it from: https://docs.python.org/2.7/tutorial/controlflow.html#keyword-arguments

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
  • Thanks for your response. What i wanted to know was how to pass it as a parameter and i now know it's as simple as any other type. I looked into the built-in function of the dictionary (i wasn't aware of their existence) and i found out my "questType" is a string and not a dictionary. I'm trying to find out why know but at least i'm not stuck anymore. Thanks everybody – LionelM May 10 '15 at 13:05