美文网首页
2018-10-10 Day 09 函数基础作业

2018-10-10 Day 09 函数基础作业

作者: EryangZ | 来源:发表于2018-10-10 20:21 被阅读0次

    Day 09 函数基础作业

    1. 编写一个函数,求1+2+3+...+N

    def num_sum(n):
        sum_num = 0
        for num in range(n+1):
            sum_num += num
        return sum_num
    
    
    #例子:求1+2+...100
    print(num_sum(100))     # 5050
    

    2. 编写一个函数,求多个数中的最大值

    def max_num(*nums):
        max_n = nums[0]
        for num in nums:
            if num > max_n:
                max_n = num
        return max_n
    
    
    # 例子:求2, 36, 67, 99, 33, 44中最大值
    print(max_num(2, 36, 67, 99, 33, 44))   # 99
    

    3. 编写一个函数,实现摇骰子的功能,打印n个骰子的点数和

    def sum_dice(n):
        import random
        sum_num = 0
        while n:
            num = random.randint(1, 6)
            print(num)  #打印出每次摇骰子的值,便于理解
            sum_num += num
            n -= 1
        return sum_num
    
    
    # 例子:打印投6次骰子的和
    print(sum_dice(6))  # 随机
    

    4. 编写一个函数,交换指定字典的key和value。

    def change_num(dict_a):
        dict_b = {}
        for dict_key in dict_a:
            dict_b[dict_a[dict_key]] = dict_key
        return dict_b
    
    
    #例子
    dict1 = {"a": 1, "b": 2, "c": 3}
    print(change_num(dict1))     # {1: "a", 2: "b", 3: "c"}
    

    5. 编写一个函数,提取指定字符串中的所有的字母,然后拼接在一起后打印出来

    # method_one
    def joint_str(str_a):
        str_print = ""
        for str_x in str_a:
            if str_x.isalpha():
                str_print += str_x
        return str_print
    
    
    # method_two
    def joint_str2(str_a):
        str_print2 = ""
        for str_x2 in str_a:
            if ("a" <= str_x2 <= "z") or ("A" <= str_x2 <= "Z"):
                str_print2 += str_x2
        return  str_print2
    
    
    
    # 例子:
    a = "21werty3abc"
    print(joint_str(a))     # wertyabc
    print(joint_str2(a))
    

    6. 写一个函数,求多个数的平均值

    def average_nums(*n):
        average_num = sum(n) / len(n)
        return  average_num
    
    
    # 例子:求 5, 10, 15的平均值
    print(average_nums(5, 10, 15))      # 10.0
    

    7. 写一个函数,默认求10的阶层,也可以求其他数的阶层

    def factorial(n):
        factorial_n = 1
        for num in range(1, n+1):
            factorial_n *= num
        return factorial_n
    
    
    # 例子:求10的阶乘
    print(factorial(10))    # 3628800
    

    8. 写一个函数,可以对多个数进行不同的运算(所有运算都用上)

    def operations(mark, num1, *nums):
        sum_nums = num1
        for num in nums:
            if mark == "+":
                sum_nums += num
            elif mark == "-":
                sum_nums -= num
            elif mark == "*":
                sum_nums *= num
            elif mark == "/":
                sum_nums /= num
            elif mark == "//":
                sum_nums //= num
            elif mark == "**":
                sum_nums **= num
        return sum_nums
    
    
    # 例子:10, 2, 2 之间用+  -  *  /  **  // 运算
    print(operations("+", 10, 2, 2))    # 14
    print(operations("-", 10, 2, 2))    # 6
    print(operations("*", 10, 2, 2))    # 40
    print(operations("/", 10, 2, 2))    # 2.5
    print(operations("**", 10, 2, 2))   # 10000
    print(operations("//", 10, 2, 2))   # 2
    

    9.写一个函数,求指定列表中,指定的元素的个数

    def list_count(list_x, n):
        counts_num = list_x.count(n)
        return counts_num
    
    
    # 例子:a = [1, 2, 1, 2, 1, 2, 3, 4, 1, 1, 1] 求1的个数
    a = [1, 2, 1, 2, 1, 2, 3, 4, 1, 1, 1]
    print(list_count(a, 1))     # 6
    

    10.写一个函数,获取指定列表中指定元素对应的下标(如果有多个,一起返回)

    def index_count(list_x, n):
        list_index = []
        for num in range(len(list_x)):
            if list_x[num] == n:
                list_index.append(num)
        return  list_index
    
    
    # 例子:a = [1, 2, 2, 1, 2, 3, 4, 1, 1] 求1的下标
    a = [1, 2, 2, 1, 2, 3, 4, 1, 1]
    print(index_count(a, 1))    #[0, 3, 7, 8]
    

    相关文章

      网友评论

          本文标题:2018-10-10 Day 09 函数基础作业

          本文链接:https://www.haomeiwen.com/subject/aapmaftx.html