Python 入门之函数结构

  1. 函数的参数 - 搭建函数房子的砖
# coding=utf-8

# 创建一个空列表numbers
numbers = []

# str用来存储输入的数字字符串,lst1是将输入的字符串用空格分割,存储为列表
str = input()
lst1 = str.split(' ')

# 将输入的数字字符串转换为整型并赋值给numbers列表
for i in range(len(lst1)):
   numbers.append(int(lst1.pop()))

# 请在此添加代码,对输入的列表中的数值元素进行累加求和
########## Begin ##########
def counts(*numbers):
    add=0
    for i in numbers:
        add +=i
    return(add)
d = counts(*numbers)
########## End ##########

print(d)


  1. 函数的返回值 - 可有可无的 return
# coding=utf-8

# 输入两个正整数a,b
a = int(input())
b = int(input())

# 请在此添加代码,求两个正整数的最大公约数
########## Begin ##########
def gcd(a,b):
    if a<b:
        t = a
        a = b
        b = t
    while b:
        t = a%b
        a = b
        b = t
    return a
########## End ##########

# 调用函数,并输出最大公约数
print(gcd(a,b))



  1. 函数的使用范围:Python 作用域
# coding=utf-8

# 输入两个正整数a,b
a = int(input())
b = int(input())
# 请在此添加代码,求两个正整数的最小公倍数
########## Begin ##########
def lcm(a,b):
    c=1
    while a:
        
        if c%a==0 and c%b==0:
            break
        c=c+1
    return c
########## End ##########

# 调用函数,并输出a,b的最小公倍数
print(lcm(a,b))


你可能感兴趣的:(educoder,python)