Python Forum

Full Version: need help for dict in function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import random
## create a list initilizae
surverylist=[]

no_of_participant=2
def checkqn(qn,surveydata): # should i do this instead?-> def checkqn(qn)
if qn==1:
Name=raw_input("What is your name:")
surveydata['name']=Name



elif qn==2:
Age=raw_input("What is your Age:")
surveydata['age']=Age



i=1
print ("no_of_participant %s" %str(i+1))
surveydata={}
checkqn(1) #should i do this instead? checkqn(1,surveydata)
checkqn(2,surveydata)
surverylist.append(surveydata)

Questions
I am not sure how dict pass value to a function
Should i pass arg with the dict to a function?

checkqn(1) or checkqn(1,surveydata)
Please, use BBcode with Python code tags for your code.

The dictionary is just an object like any other Python primitive. You just pass it to a function.
def func (a_dict):
    if 'No more' in a_dict:
        print('I want more!')
    
    return False

my_dict = {'one': 1, 'two': 2, 'No more': 'Sorry!'}

func (my_dict) # calling the function and pass the dictionary