美文网首页
2019-01-04作业

2019-01-04作业

作者: 白与黑_d83f | 来源:发表于2019-01-04 22:54 被阅读0次

import random

  1. 编写一个函数,求1+2+3+...+N
def sum_n(n):
    s = 0
    for i in range(1, n+1):
        s += i
    print(s)
sum_n(10)
  1. 编写一个函数,求多个数中的最大值
def max_muti(*args):
    print(max(args))
max_muti(1, 2, 3, 45, 6, 7)
  1. 编写一个函数,实现摇色子的功能,打印n个色子的点数和
def sum_dice(n : int):
    dice = []
    s = 0
    for i in range(1, n+1):
        dice.append(random.randint(1, 6))
    s += sum(dice)
    print(s)
sum_dice(5)
  1. 编写一个函数,交换指定字典的key和value。
    例如:{'a':1, 'b':2, 'c':3} ---> {1:'a', 2:'b', 3:'c'}
def exchange_key_value(n:dict):
    new_dict = {}
    for key in n:
        new_dict[n[key]] = key
    print(new_dict)
exchange_key_value({'a':1, 'b':2, 'c':3})
  1. 编写一个函数,三个数中的最大值
def max_three_num(a, b, c):
    m = 0
    list1 = []
    list1.append(a)
    list1.append(b)
    list1.append(c)
    m = max(list1)
    print(m)
max_three_num(110, 256, 389)
  1. 编写一个函数,提取指定字符串中的所有的字母,然后拼接在一起后打印出来
    例如:'12a&bc12d--' ---> 打印'abcd'
def alphabet(s: str):
    new_list = []
    for i in (s):
        if 'a' <= i <= 'z' or 'A' <= i <= 'Z':
            print(i, end = '')
  1. 写⼀一个函数,求多个数的平均值
def mean(*args):
    list1 = list(args)
    m = sum(list1) / (len(list1))
    print(m)
mean(5,10,15,100,555)
alphabet('12a&bc12d--')
  1. 写一个函数,默认求10的阶层,也可以求其他数的阶层
def fac(n : int):
    f = 1
    for i in range(1, n+1):
        f *= i
    print(f)
fac(10)
  1. 写⼀一个函数,可以对多个数进行不同的运算
    例例如: operation('+', 1, 2, 3) ---> 求 1+2+3的结果
    operation('-', 10, 9) ---> 求 10-9的结果
    operation('', 2, 4, 8, 10) ---> 求 24810的结构
def operation(operator, *args):
    print(operator, args)
    if operator == '+':
        s = sum(args)
        print(s)
    if operator =='-':
        substraction=0
        for i in args[:]:
            substraction -= i
        subs=substraction+args[0]
        print(subs)
    if operator == '*':
        multi = 1
        for i in (args):
            multi *= i
        print(multi)
operation('-',1,2,3,4,5)

相关文章

  • 2019-01-04

    2019-01-04桓台台姜博士眼镜商迎新 2019-01-04 桓台姜博士眼镜商迎新 姜博士眼镜商迎新。大家好!...

  • 2019-01-04作业

    1.编写一个函数,求1+2+3+...+N 2.编写一个函数,求多个数中的最大值 3.编写一个函数,实现摇⾊子的功...

  • 2019-01-04作业

    import random 编写一个函数,求1+2+3+...+N 编写一个函数,求多个数中的最大值 编写一个函数...

  • 晨读DAY 12 总结日志

    ScalersTalk千人早起晨读团 [Day 12 2019-01-04] Materials: The stu...

  • 2019-01-04实修作业

    自我滋养 亲爱的志坚,你可以爱你自己,因为你是值得的,面对生活,请允许自己出错,当然你也可以不高兴,但你更应该好好...

  • 2019-01-4  文章

    可惜不是你,陪我到最后。 阿丟 0.133 · 字数 1562 · 阅读 75 2019-01-04 19:28 ...

  • 荆的ScalersTalk第四轮新概念朗读持续力训练Day88

    20190104周五Day88 练习材料: 原文[Day 88 2019-01-04]Lesson89 A sli...

  • 2019-01-04

    梅梦春花蝶翻飞 平常人15564010639拒 字数 126 · 阅读 0 2019-01-04 08:26 雪摧...

  • Mac 搭建 rtmp直播服务器小记

    查阅了很多博客,找到可行的方案,做个记录,方便下次查看。 2019-01-04 mac使用brew update无...

  • IOS和Android下汇总

    项目问题 IOS下 时间问题: '2019-01-04' 在ios下不能被解析, '2019/01/04'才行 ...

网友评论

      本文标题:2019-01-04作业

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