Missing 3 required positional arguments Python

99封情书 提交于 2021-02-04 21:54:32

问题


Right so I'm working on a python code and I get this type error, "TypeError: printE() missing 3 required positional arguments: 'emp2', 'emp3', and 'emp4'"

for emmp in employee:
    print(printE(emmp))

def printE(emp1, emp2, emp3, emp4):

    emp1 = "{}, {}, {}, {}".format(emp1[0], ' '.join(emp1[1:-2]))
    emp2 = "{}, {}, {}, {}".format(emp2[1], ' '.join(emp2[2:-3]))
    emp3 = "{}, {}, {}, {}".format(emp3[2], ' '.join(emp3[3]))
    emp4 = "{}, {}, {}, {}".format(emp4[3], ' '.join(emp4[0:-1]))
    print("{:10s} {:15s} {:5s} {:15s}".format(emp4[0], emp1[1], emp2[2], emp3[3]))

Any sort of help will be much appreciated!


回答1:


for emmp in employee:
    print(printE(emmp))

as you say employee is tuple like

Case 1

employee = ('E1','E2'.....)

Note: when you iterator over tuple using for loop it gives you single employ E1 or next time E2 so on

And your function printE takes four arguments and you call it with the only single argument 'E1' or next time 'E2' so on.

So it gives you error that remaining argument are missing.

Case 2

if employee is tuple of tuple then look this example

employee = (('E1',10,"b10",20),('E1',10,"b10",20))

def printE(emp1, emp2, emp3, emp4):
    """ do what ever you want to do with param meters """

    return emp1 ,emp2 ,emp3, emp4

for emmp in employee:
    print(printE(*emmp))

Output

('E1', 10, 'b10', 20)                                                                                                   

('E1', 10, 'b10', 20)                                                                                                   



回答2:


in a for loop, you will get one employee at a time. so the function is getting only one argument. The error is clear, that 3 arguments are missing. try passing the employees directly into the function




回答3:


printE method takes 4 arguments and in your loop your only giving one. Or if in loop 'emmp' is a list of four elements. Then try calling like 'printE(*emmp)'.



来源:https://stackoverflow.com/questions/45853466/missing-3-required-positional-arguments-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!