Error Collections

Positional Argument Follows Keyword Argument In Python

yunajoe 2022. 10. 9. 12:22

When to occur?!

- added_score함수가 2개의 parameter(answer, result)를 받는데 함수를 사용할 때 added_score(answer= answer_report, result), 즉 첫번째에는 positional argument 를 사용하고 두번째에는 사용하지 않았음

scores = {1:3, 2:2, 3:1, 4:0, 5:1, 6:2, 7:3}    
answer_report = {1: {"R":0, "T":0}, 
  2 :{"C":0, "F":0}, 
  3: {"J":0, "M":0}, 
  4: {"A":0, "N":0},  
}  

def organization(answer):
    answer = {k : dict(sorted(v.items(), key=lambda x:(-x[1], x[0]))) for k, v in answer.items()}
    return "".join([list(v.keys())[0] for k, v in answer.items()])


def added_score(answer, result:dict) -> dict:
    for k, v in answer.items():
        for type, score in result.items():
            if type in v:
                v[type] = score
    return answer 

def solution(survey, choices):   
    result = {}
    for type, choice in zip(survey, choices):
        if choice in range(1,4):  # 비동일 경우 첫번째 
            result[type[0]] = scores[choice]          
        elif choice in range(5,8): # 동의일 경우 두번째
            result[type[1]] = scores[choice]  
    return added_score(answer=answer_report,result)

 

How to fix it?!

- positional arguments는 항상 keyword arguments보다  뒤에 와야 한다 

def solution(survey, choices):   
    result = {}
    for type, choice in zip(survey, choices):
        if choice in range(1,4):  # 비동일 경우 첫번째 
            result[type[0]] = scores[choice]          
        elif choice in range(5,8): # 동의일 경우 두번째
            result[type[1]] = scores[choice]  
    return added_score(answer=answer_report,result=result)