๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[python] ๋ฐฐ์—ด์— ๊ฐ’ ์ถ”๊ฐ€ ๋ณธ๋ฌธ

python

[python] ๋ฐฐ์—ด์— ๊ฐ’ ์ถ”๊ฐ€

Shinjio 2023. 8. 24. 08:20

append()

  • ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ง€์ง€ ์•Š์€ ๋ฐฐ์—ด์— ์ธ๋ฑ์Šค ์‚ฌ์šฉํ•˜๋ฉด ์˜ค๋ฅ˜ ๋ฐœํ–‰ํ•จ
  • → IndexError: list assignment index out of range
  • ๋”ฐ๋ผ์„œ ํฌ๊ธฐ๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์€ ๋ฐฐ์—ด์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” append()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

 

 

def solution(num_list):
    answer = []
    odd = 0
    even = 0
    
    for i in num_list:
        if i % 2 == 0 :
            even += 1
        else:
            odd += 1
    
    answer.append(even)
    answer.append(odd)
    
    return answer