#找最大值和最小值
def find_max_and_min(array): #define function 函数名(形式参数):
min = array[0]
max = array[0]
for i in array:
if i > max:
max = i
if i < min:
min = i
print("最小值:" + str(min))
print("最大值:" + str(max))
return min, max #函数结束
array = [1,2,3,4,8,6,9,10]
a,b = find_max_and_min(array)
print("最小值:" +str(a))
print("最大值:" +str(b))
#找第一个数在数组中相同数的个数
def find_same(array):
same = array[0]
answer = 0
for i in array:
if i == same:
answer = answer+1
return answer
array = [2,3,4,5,67,3,2,121,2,3,54,6]
answer = find_same(array)
print ('一共有:'+str(answer)+'个')
#求数组中所有元素相同的个数 缩进有问题!!!!!
def find_all_same(array):
for i in array:
answer = 0
for j in array:
if i == j:
answer = answer + 1
print('数字'+str(i)+'共有'+str(answer)+'个')
array = [11,2,2,4,5,6,6,78,2,11,34,5]
find_all_same(array)
改正后的::::
#求数组中所有元素相同的个数
def find_all_same(array):
for i in array:
answer = 0
for j in array:
if i == j:
answer = answer + 1
print('数字'+str(i)+'共有'+str(answer)+'个')
array = [11,2,2,4,5,6,6,78,2,11,34,5]
find_all_same(array)
#每个函数只执行一条return语句,表示函数结束(可以有多条return语句)
#求数组中所有元素相同的个数
def find_all_same(array):
for i in array:
answer = 0
for j in array:
if i == j:
answer = answer + 1
if answer >= 3:
return None
print('数字'+str(i)+'共有'+str(answer)+'个')
array = [11,2,2,4,5,6,6,78,2,11,34,5]
find_all_same(array)
#每个函数只执行一条return语句,表示函数结束(可以有多条return语句)
#有三个一样的数就结束这个程序
?
?
|