-1
 def bowling(p1,p2,p3,p4,p5):
        x=1
        while x<6:
            print(px.get('role'))   

"NameError: name 'P' is not defined"

 x=x+1
 p1={'name':'Virat Kohli', 'role':'bat', 'runs':112, '4':10, '6':0, 'balls':119, 'field':0}
 p2={'name':'du Plessis', 'role':'bat', 'runs':120, '4':11, '6':2, 'balls':112, 'field':0}
 p3={'name':'Bhuvneshwar Kumar', 'role':'bowl', 'wkts':1, 'overs':10, 'runs':71, 'field':1}
 p4={'name':'Yuzvendra Chahal', 'role':'bowl', 'wkts':2, 'overs':10, 'runs':45, 'field':0}
 p5={'name':'Kuldeep Yadav', 'role':'bowl', 'wkts':3, 'overs':10, 'runs':34, 'field':0}
 bowling(p1,p2,p3,p4,p5)    #passing dict as argument
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
preet parikh
  • 29
  • 1
  • 7

1 Answers1

1

python can not access variables the way you think it does... (with x=1 python will not be able to access p1 when you write px; px is just a new name that has not been defined yet).

what you could do is this:

def bowling(*args):
    for px in args:
        print(px.get("role"))

bowling(p1, p2, p3, p4, p5)

i.e. use a variable number of arguments *args and iterate over them. you can find more information on how to use those in the python documentation or here or here for example.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111