Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Leading engineer

함수 만들기 본문

Language/Python

함수 만들기

Daisy_EngineerJH 2023. 2. 3. 17:19

for문을 사용하여 A학급의 평균 점수를 구해보세요

class_A = [70,60,55,75,95,90,80,80,85,100]

def class_avg(class_A):

    for i in range(1,10) :
        total = total + class_A
        avg = total / 10

    return(class_avg)

class_avg()

이렇게 했더니

# TypeError: class_avg() missing 1 required positional argument: 'class_A' 라는 오류메시지가 나왔다...

아래는 코드작성 과정

과정1

def class_avg(*args):


    for i in args :
    for i in class_A :

        total = 0
        total = total + class_A
@@ -20,4 +20,4 @@ def class_avg(*args):

# TypeError: class_avg() missing 1 required positional argument: 'class_A'
# UnboundLocalError: cannot access local variable 'total' 
#                      where it is not associated with a value
#                      where it is not associated with a value

과정2

lass_A = [70,60,55,75,95,90,80,80,85,100]

def class_avg(*args):


    for i in class_A :

        total = 0
        total = total + class_A
def class_avg(*agrs):
     total= 0
     for i in args :
        total = total + i
        avg = total / 10

    return total
     return avg
     print(avg)

class_avg()

해결

# 함수 만들기


# for문을 사용하여 A학급의 평균 점수를 구해보세요

class_A = [70,60,55,75,95,90,80,80,85,100]
args = 0

def class_avg(i, class_A=None):
     total= 0
     if class_A is None:
            class_A=[]
            class_A.append(i)
            for class_A in i :
                total = total + class_A
                avg = total / 10
     return avg
     print(avg)

print(class_avg(class_A))

# TypeError: class_avg() missing 1 required positional argument: 'class_A'
# UnboundLocalError: cannot access local variable 'total' 
#                      where it is not associated with a value
# TypeError: 'int' object is not iterable

'Language > Python' 카테고리의 다른 글

python if문  (0) 2023.02.01
문자열  (0) 2023.02.01
자료형과 기본연산  (0) 2023.02.01
변수  (0) 2023.01.30
기본 자료형  (0) 2023.01.30
Comments