美文网首页
day8-作业

day8-作业

作者: 2333_11f6 | 来源:发表于2018-11-14 20:55 被阅读0次
    1. 编写⼀个函数,求1+2+3+...+N
      程序:
    # 1. 编写⼀个函数,求1+2+3+...+N
    def sum1(N):
    """求1+2+3+...+N"""
        sum2 = 0
        for i in range(1, N + 1):
            sum2 += i
        print(sum2)
    
    
    sum1(10)
    sum1(11)
    

    结果:


    运行结果
    1. 编写⼀个函数,求多个数中的最⼤值
      程序:
    #2. 编写⼀个函数,求多个数中的最⼤值
    def my_max(list1):
    """求多个数中的最⼤值"""
        print('需要比较的数为:', list1)
        print('它们中最大值为:', max(list1))
    
    
    list1 = []
    while 1:
        float1 = input('请输入需要比较的数字,输入None表示输入完成:')
        if float1 != 'None':
            list1.append(float(float1))
        else:
            break
    
    my_max(list1)
    

    结果:


    运行结果
    1. 编写⼀个函数,实现摇⾊⼦的功能,打印n个⾊⼦的点数和
      程序:
    #3. 编写⼀个函数,实现摇⾊⼦的功能,打印n个⾊⼦的点数和
    import random
    
    
    def roll_dice():
    """生成随机数1-6,即摇⾊⼦"""
        return random.randint(1, 6)
    
    
    list1 = []
    n = int(input('请输入摇色子的次数:'))
    for _ in range(n):
        list1.append(roll_dice())
    print('所有的点数为:', list1)
    print('它们的和为:', sum(list1))
    

    结果:


    运行结果
    1. 编写⼀个函数,交换指定字典的key和value。
      例如:{'a':1, 'b':2, 'c':3} ---> {1:'a', 2:'b', 3:'c'}
      程序:
    #4. 编写⼀个函数,交换指定字典的key和value。
    # 例如:{'a':1, 'b':2, 'c':3} ---> {1:'a', 2:'b', 3:'c'}
    
    
    def exchange(dict1):
        """交换dict1的key和value值,仅适用于value值不等的情况"""
        list_key = list(dict1.keys())
        list_value = list(dict1.values())
        list2 = []
        for index in range(len(list_key)):
            list1 = [list_value[index], list_key[index]]
            list2.append(list1)
    
        print(dict(list2))
    
    
    dict1 = {'a': 1, 'b': 2, 'c': 3}
    exchange(dict1)
    

    结果:


    运行结果
    1. 编写⼀个函数,三个数中的最⼤值
      程序:
    #5. 编写⼀个函数,求三个数中的最⼤值
    
    
    def my_max(num1, num2, num3):
        """取3数中最大值"""
        print(max(num1, num2, num3))
    
    
    my_max(77, 44, 66)
    my_max(45, 34, 23)
    my_max(12, 33, 22)
    

    结果:


    运行结果
    1. 编写⼀个函数,提取指定字符串中的所有的字⺟,然后拼接在⼀起后打印出来
      例如: '12a&bc12d--' ---> 打印'abcd'
      程序:
    #6. 编写⼀个函数,提取指定字符串中的所有的字⺟,然后拼接在⼀起后打印出来
    #'12a&bc12d--' ---> 打印'abcd'
    #1
    def extract_the_letters(str1):
        """提取字符串str1中的字母并打印"""
        list1 = list(str1)
        list2 = []
        for index in range(len(list1)):
            if list1[index].isalpha():      # 判断是否为字母
                list2.append(list1[index])
    
        str2 = ''
        for index in range(len(list2)):
            str2 += list2[index]
    
        print(str2)
    
    
    str1 = '12a&bc12d--'
    extract_the_letters(str1)
    
    #2
    
    
    def extract_the_letters(str1):
        """提取字符串str1中的字母并打印"""
        str2 = ''
        for char1 in str1:
            if char1.isalpha():  # 判断是否为字母
                str2 += char1
    
        print(str2)
    
    
    str1 = '12a&bc12d--'
    extract_the_letters(str1)
    

    结果:


    运行结果
    1. 写⼀个函数,求多个数的平均值
      程序:
    #7. 写⼀个函数,求多个数的平均值
    def average(list1):
        print('它们的平均值为:%.3f' %(sum(list1)/len(list1)))
    
    
    list1 = []
    while 1:
        float1 = input('请输入需要求平均值的数字,输入None表示输入完成:')
        if float1 != 'None':
            list1.append(float(float1))
        else:
            break
    
    average(list1)
    

    结果:


    运行结果
    1. 写⼀个函数,默认求10的阶层,也可以求其他数的阶乘
      程序:
    # 8. 写⼀个函数,默认求10的阶层,也可以求其他数的阶乘
    # 8. 写⼀个函数,默认求10的阶层,也可以求其他数的阶乘
    
    
    def factorial(num1=10):        # 设置默认参数
        """求num1的阶乘,默认为10"""
        factorial1 = 1
        i = 1
        while i <= num1:
            factorial1 *= i
            i += 1
    
        print('%d的阶乘为:%d' % (num1, factorial1))
    
    
    factorial()
    factorial(11)
    factorial(12)
    

    结果:


    运行结果
    1. 写⼀个函数,可以对多个数进⾏不同的运算
      例如: operation('+', 1, 2, 3) ---> 求 1+2+3的结果
      operation('-', 10, 9) ---> 求 10-9的结果
      operation('', 2, 4, 8, 10) ---> 求 24810的结果
      程序:
    #9. 写⼀个函数,可以对多个数进⾏不同的运算
    # 例如: operation('+', 1, 2, 3) ---> 求 1+2+3的结果
    # operation('-', 10, 9) ---> 求 10-9的结果
    # operation('*', 2, 4, 8, 10) ---> 求 2*4*8*10的结果
    
    
    def operation(*tuple1):       # 传入多个参数
        """根据输入的参数进行相应的运算"""
        operation1, *nums = tuple1
        if operation1 == '+':
            sum1 = 0
            for item in nums:
                sum1 += item
            print(sum1)
        elif operation1 == '-':
            sub1 = 2*nums[0]
            for item in nums:
                sub1 -= item
            print(sub1)
        elif operation1 == '*':
            mul1 = 1
            for item in nums:
                mul1 *= item
            print(mul1)
        elif operation1 == '/':
            div1 = nums[0]**2
            for item in nums:
                div1 /= item
            print(div1)
        else:
            print('运算符输入错误!')
    
    
    operation('+', 1, 2, 3)
    operation('+', 1, 2, 3, 4)
    operation('-', 10, 9, -2)
    operation('*', 2, 4, 8, 10)
    operation('/', 8, 4, 2, 1, 0.1)
    

    结果:


    运行结果

    相关文章

      网友评论

          本文标题:day8-作业

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