美文网首页
Day09-作业

Day09-作业

作者: 萌王史莱姆 | 来源:发表于2019-01-04 20:37 被阅读0次

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

    def gyh_sun(num: int):
        """求从1加2加3...加到一个数的和"""
        sum1 = 0
        for x in range(1, num + 1):
            sum1 += x
        return sum1
    
    
    input_num = int(input('请输入一个数:'))
    result = gyh_sun(input_num)
    print('从1加到%d的和为%d' % (input_num, result))
    

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

    def max_num(*args):
        """求多个数中的最大值"""
        the_max = max(args)
        return the_max
    
    
    result = max_num(2, 5, 8, 9)
    print('最大值为%d' % result)
    

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

    import random
    
    
    def dice_sum(n: int):
        """实现摇色子的功能,打印n个色子的点数和"""
        sum1 = 0
        for _ in range(n):
            sum1 += random.randint(1, 6)
    
        return sum1
    
    
    n = int(input('请输入摇色子的个数:'))
    result = dice_sum(n)
    print('%d个色子的点数和为%d' % (n, result))
    

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

    例如:{'a':1, 'b':2, 'c':3} ---> {1:'a', 2:'b', 3:'c'}

    def change_key_value(dict1: dict):
        """交换指定字典的key和value"""
        dict3 = {}
        for key in dict1:
            a = dict1[key]
            dict3[a] = key
        return dict3
    
    
    dict2 = {'a': 1, 'b': 2, 'c': 3}
    result = change_key_value(dict2)
    print(result)
    

    5. 编写⼀个函数,三个数中的最大值

    def max_num(*args):
        the_max = max(args)
        return the_max
    
    
    result = max_num(5, 8, 9)
    print("三个数中我的最大值为%d" % result)
    

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

    例如:'12a&bc12d--' ---> 打印'abcd'

    def joint_letter(str1: str):
        """提取指定字符串中的所有的字母,然后拼接在⼀起后打印出来"""
        str2 = ''
        for index in range(len(str1)):
            if 'a' <= str1[index] <= 'z' or 'A' <= str1[index] <= 'Z':
                str2 += str1[index]
        return str2
    
    
    result = joint_letter('fegb;;./efeFg5AD4gedFs52G2fe')
    print('拼接在一起的字符串为%s' % result)
    

    7. 写⼀个函数,求多个数的平均值

    def aver_val(*args):
        aver = sum(args) / len(args)
        return aver
    
    
    result = aver_val(5, 8, 50, 56, 200)
    print('平均值为%.2f' % result)
    

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

    def factorial(num=10):
        """默认求10的阶层,也可以求其他数的阶层"""
        product = 1
        for item in range(1, num + 1):
            product *= item
        return num, product
    
    
    input_num = int(input('请输入一个整数:'))
    if input_num == 10:
        num1, result = factorial()
        print('%d的阶乘为%d' % (num1, result))
    else:
        num1, result = factorial(input_num)
        print('%d的阶乘为%d' % (num1, result))
    

    9. 写⼀个函数,可以对多个数进⾏不同的运算

    例如: operation('+', 1, 2, 3) ---> 求 1+2+3的结果
    operation('-', 10, 9) ---> 求 10-9的结果
    operation('', 2, 4, 8, 10) ---> 求 24810的结构

    def operation(str1: str, *args):
        result1 = 0
        result2 = 1
        if str1 == '+':
            for item in args:
                result1 += item
            return result1
        if str1 == '*':
            for item in args:
                result2 *= item
            return result2
        if str1 == '-':
            result1 = args[0]
            for item in args[1:]:
                result1 -= item
            return result1
        if str1 == '/':
            result2 = args[0]
            for item in args[1:]:
                result2 /= item
            return result2
        if str1 == '%':
            result2 = args[0]
            for item in args[1:]:
                result2 %= item
            return result2
        if str1 == '//':
            result2 = args[0]
            for item in args[1:]:
                result2 //= item
            return result2
        if str1 == '**':
            result2 = args[0]
            for item in args[1:]:
                result2 **= item
            return result2
    
    
    result = operation('+', 2, 2, 2)
    print('结果为%.2f' % result)
    

    相关文章

      网友评论

          本文标题:Day09-作业

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