关于python的全局变量和局部变量的理解(分解问题来排错)
list_1 = [ ] num = input ( "请输入数字:" ) while num != "" : list_1 . append ( num ) num = input ( "请输入数字:" ) #上述5条代码是重点要理解:定义一个全局变量list_1 """ 求和 """ def sum_1 ( ) : sum = 0 for i in list_1 : sum += eval ( i ) return sum """ 求长度 """ def len_1 ( ) : count = 0 for i in list_1 : count = count + 1 lenth = count return lenth """ 平均数 """ def averange ( ) : aver = sum_1 ( ) / len_1 ( ) return aver print ( averange ( ) ) #float """ 方差 """ def fang_cha ( ) : fc = 0 for i in list_1 : #之所以不是list_2,是因为它是局部变量,若写list_2,则list_2是空列表 a = float ( i ) b = a - averange ( ) # print(type(averange())) fc += pow ( b , 2 )