美文网首页
2018-10-10-day9Homework

2018-10-10-day9Homework

作者: 芽菜包66 | 来源:发表于2018-10-10 20:29 被阅读0次

    10.10 函数基础作业

    1. 编写一个函数,求1+2+3+...+N
     def my_sum(n):  
         sum1 = 0
         for x in range(1,n+1):
             sum1 += x
         return sum1
     print(my_sum(5))
    

    结果:15

    1. 编写一个函数,求多个数中的最大值
     def my_max(*num):
         max = 0
         for x in num:
             if x >max:
                 max = x
         return max
     print(my_max(5,64,67,13,84,64,90,31,47))
    

    结果:90

    1. 编写一个函数,实现摇骰子的功能,打印n个骰子的点数和
     def tou_zi(n):
         import random
         i = 0
         sum1 = 0
         while i < n:
             x = random.randint(1,6)
             sum1 += x
             i += 1
         return sum1
     print(tou_zi(2))
    
    1. 编写一个函数,交换指定字典的key和value。如:{'a':1, 'b':2, 'c':3} ---> {1:'a', 2:'b', 3:'c'}
    def change_keyvalue(dict1):
        dict2={}
        for index1 in dict1:
            value = index1
            key = dict1[index1]
            dict2.setdefault(key,value)
        return dict2
    print(change_keyvalue({'name':'hqf','sex':'man','age':18}))
    
    1. 编写一个函数,提取指定字符串中的所有的字母,然后拼接在一起后打印出来
      如:'12a&bc12d--' ---> 打印'abcd
     def get_alp(n):
         new_str = ''
         for x in n:
             if x.isalpha():
                 new_str += x
         return new_str
     print(get_alp('d4sa564das6'))
    

    结果:dasdas

    1. 写一个函数,求多个数的平均值
     def my_avrage(*n):
         sum = 0
         num = 0
         avr = float(0)
         for x in n:
             sum += x
             num +=1
         avr = sum/num
         return avr
     print(my_avrage(5,654,2,78,312,4))
    

    结果:175.83

    1. 写一个函数,默认求10的阶层,也可以求其他数的阶层
     def my_jiecheng(n=10):
         jiecheng = 1
         for x in range(1,n+1):
             jiecheng *= x
         return jiecheng
     print(my_jiecheng())
    

    结果:3628800

    1. 写一个函数,可以对多个数进行不同的运算
      如: operation('+', 1, 2, 3) ---> 求 1+2+3的结果
      operation('-', 10, 9) ---> 求 10-9的结果
      operation('', 2, 4, 8, 10) ---> 求 24810的结构
     def my_suanfa(list1):
         num1 = 0
         if list1[0] == '+':
             del list1[0]
             answer = list1[0]
             for index in range(0,len(list1)):
                 num1 = list1[index]
                 answer += num1
         elif list1[0] == '-':
             del list1[0]
             answer = list1[0]
             for index in range(0,len(list1)):
                 num1 = list1[index]
                 answer -= num1
         else:
             del list1[0]
             answer = list1[0]
             for index in range(0, len(list1)):
                 num1 = list1[index]
                 answer *= num1
         return answer
     print(my_suanfa(['+',1,2,3,4,5]))
     print(my_suanfa(['-',1,2,3,4,5]))
     print(my_suanfa(['*',1,2,3,4,5]))
    

    结果:16,-14,120
    9.写一个函数,求指定列表中,指定的元素的个数

     def my_count(list1,str1):
         count1 = 0
         for x in list1:
             if x == str1:
                 count1 += 1
         return count1
     print(my_count([1,2,3,4,1,2,3,1,2,1],3))
    

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

     def my_index(list1,str1):
         num =''
         for index1 in range(0,len(list1)):
             if list1[index1] == str1:
                 num += 'index1'
                 print(index1)
     my_index([1, 1, 2, 3, 4, 4, 3, 2, ], 2)
    

    结果:2、7

    相关文章

      网友评论

          本文标题:2018-10-10-day9Homework

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