Leading engineer
함수 만들기 본문
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
Comments