Python Forum
need help for dict in function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help for dict in function
#1
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)
Reply
#2
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a dict in dict cherry_cherry 4 74,631 Apr-08-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020