esc0rp 发表于 2024-8-18 05:28:41

Python语法示例——函数


    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><span style="color: black;">12_Python语法示例(函数)</span></span></h1>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/pgc-image/674cdd3af27142f49025ea1815c84868~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723899749&amp;x-signature=xh%2BWtTtUHnipF3dQCRddfQtExpE%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1.写一个函数mysum,此函数带有两个参数x,y打印出两个参数x,y的和</span></h1><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">mysum</span><span style="color: black;">(x, y)</span>:</span>
    print(x + y)


    mysum(<span style="color: black;">3</span>, <span style="color: black;">2</span>)<h1 style="color: black; text-align: left; margin-bottom: 10px;">2.写一个函数print_even,传入一个数参n<span style="color: black;">表率</span>终值整数(不<span style="color: black;">包括</span>n),打印0-n之间所有偶数</h1><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">print_even</span><span style="color: black;">(n)</span>:</span>
    print(list(x <span style="color: black;">for</span> x <span style="color: black;">in</span> range(n) <span style="color: black;">if</span> x % <span style="color: black;">2</span> == <span style="color: black;">0</span>))


    print_even(<span style="color: black;">10</span>)<h1 style="color: black; text-align: left; margin-bottom: 10px;">3.写一个函数mymax,返回两个数的最大值</h1><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">mymax</span><span style="color: black;">(a, b)</span>:</span>
    <span style="color: black;">return</span> a <span style="color: black;">if</span> a &gt; b <span style="color: black;">else</span> b


    print(mymax(<span style="color: black;">3</span>, <span style="color: black;">7</span>))<h1 style="color: black; text-align: left; margin-bottom: 10px;">4.写一个函数input_number用于读取输入的多个整数,输入负数时结束将输入的数形成列表返回给调用者</h1><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">input_number</span><span style="color: black;">()</span>:</span>
    L1 = list()
    <span style="color: black;">while</span> <span style="color: black;">True</span>:
    num = int(input(<span style="color: black;">"输入整数为负停止:"</span>) <span style="color: black;">or</span> <span style="color: black;">"-1"</span>)
    <span style="color: black;">if</span> num &gt;= <span style="color: black;">0</span>:
    L1.append(num)<span style="color: black;">else</span>:
    <span style="color: black;">break</span>
    <span style="color: black;">return</span> L1


    print(input_number())<h1 style="color: black; text-align: left; margin-bottom: 10px;">5.写一个函数mysum,<span style="color: black;">能够</span>传入任意个实参的数字,返回所有实参的和</h1><span style="color: black;">def</span> <span style="color: black;">mysum(*args):</span>
    <span style="color: black;">num</span> = <span style="color: black;">0</span>
    <span style="color: black;">for</span> <span style="color: black;">i in args:</span>
    <span style="color: black;">num</span> <span style="color: black;">+= i</span>
    <span style="color: black;">return</span> <span style="color: black;">num</span>


    <span style="color: black;">print(mysum(1,</span> <span style="color: black;">2, 3, 4))</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">6.仿照max写一个mymax函数,实现功能与max完全相同</h1><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">mymax</span><span style="color: black;">(a, *args)</span>:</span> <span style="color: black;"># *聚合</span>

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">_max</span><span style="color: black;">(*args)</span>:</span>
    m = args[<span style="color: black;">0</span>]
    i = <span style="color: black;">1</span>
    <span style="color: black;">while</span> i &lt; len(args):
    <span style="color: black;">if</span>args &gt; m:
    m = args
    i +=<span style="color: black;">1</span>
    <span style="color: black;">return</span> m

    <span style="color: black;">if</span> len(args) == <span style="color: black;">0</span>:
    <span style="color: black;">return</span> _max(*a) <span style="color: black;"># *打散</span>
    <span style="color: black;">return</span> _max(a, *args) <span style="color: black;"># *打散</span>


    print(mymax(<span style="color: black;">1</span>, <span style="color: black;">2</span>, <span style="color: black;">3</span>, <span style="color: black;">4</span>))<h1 style="color: black; text-align: left; margin-bottom: 10px;">7.写一个函数minmax,<span style="color: black;">能够</span>给出任意个数字实参,返回这些实参的最小数和最大数</h1><span style="color: black;"># <span style="color: black;">办法</span>一</span>
    <span style="color: black;">def</span> <span style="color: black;">minmax(*args):</span>
    <span style="color: black;">return</span> <span style="color: black;">min(args),</span> <span style="color: black;">max(args)</span>


    <span style="color: black;">a,</span> <span style="color: black;">b</span> <span style="color: black;">=</span> <span style="color: black;">minmax(2,</span> <span style="color: black;">1</span><span style="color: black;">,</span> <span style="color: black;">3</span><span style="color: black;">,</span> <span style="color: black;">7</span><span style="color: black;">,</span> <span style="color: black;">4</span><span style="color: black;">)</span>
    <span style="color: black;">print(a,</span> <span style="color: black;">b)</span>
    <span style="color: black;"># <span style="color: black;">办法</span>二</span>
    <span style="color: black;">def</span> <span style="color: black;">my_max_min(*args):</span>
    <span style="color: black;">args</span> <span style="color: black;">=</span> <span style="color: black;">list(args)</span>
    <span style="color: black;"># print(args)</span>
    <span style="color: black;">for</span> <span style="color: black;">i</span> <span style="color: black;">in</span> <span style="color: black;">range(0,</span> <span style="color: black;">len(args)-1):</span>
    <span style="color: black;">if</span> <span style="color: black;">args</span> <span style="color: black;">&gt;</span> <span style="color: black;">args:</span>
    <span style="color: black;">args,</span> <span style="color: black;">args</span> <span style="color: black;">=</span> <span style="color: black;">args,</span> <span style="color: black;">args</span>
    <span style="color: black;">max_num</span> <span style="color: black;">=</span> <span style="color: black;">args</span>
    <span style="color: black;">for</span> <span style="color: black;">i</span> <span style="color: black;">in</span> <span style="color: black;">range(len(args)-1,</span> <span style="color: black;">0</span><span style="color: black;">,</span> <span style="color: black;">-1</span><span style="color: black;">):</span>
    <span style="color: black;">if</span> <span style="color: black;">args</span> <span style="color: black;">&lt;</span> <span style="color: black;">args:</span>
    <span style="color: black;">args,</span> <span style="color: black;">args</span> <span style="color: black;">=</span> <span style="color: black;">args,</span> <span style="color: black;">args</span>
    <span style="color: black;">min_num</span> <span style="color: black;">=</span> <span style="color: black;">args</span>
    <span style="color: black;">print(args)</span> <span style="color: black;"># </span>
    <span style="color: black;">return</span> <span style="color: black;">(min_num,</span> <span style="color: black;">max_num)</span>


    <span style="color: black;">res</span> <span style="color: black;">=</span> <span style="color: black;">my_max_min(5,</span> <span style="color: black;">1</span><span style="color: black;">,</span> <span style="color: black;">7</span><span style="color: black;">,</span> <span style="color: black;">2</span><span style="color: black;">,</span> <span style="color: black;">4</span><span style="color: black;">,</span> <span style="color: black;">3</span><span style="color: black;">,</span> <span style="color: black;">6</span><span style="color: black;">)</span>
    <span style="color: black;">print(res)</span> <span style="color: black;"># (1, 7)</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">8.写一个函数 mysum此函数<span style="color: black;">能够</span>传入一个参数,两个参数和三个参数</h1><span style="color: black;">此函数的功能是返回从<span style="color: black;">起始</span>到终止值的和
      1) 当传入一个参数时,这个参数<span style="color: black;">表率</span>终止数
      2) 当传入两个参数时,<span style="color: black;">第1</span>个参数<span style="color: black;">表率</span><span style="color: black;">初始</span>值,第二个参数<span style="color: black;">表率</span>终止值
      3) 当传入三个参数时,第三个参数<span style="color: black;">表率</span>步长
    </span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">mysum</span><span style="color: black;">(start, stop=None, step=<span style="color: black;">1</span>)</span>:</span>
    <span style="color: black;">if</span> stop <span style="color: black;">is</span> <span style="color: black;">None</span>:
    start, stop = <span style="color: black;">0</span>, start
    num = <span style="color: black;">0</span>
    <span style="color: black;">for</span> i <span style="color: black;">in</span> range(start, stop, step):
    num += i
    <span style="color: black;">return</span> num


    print(mysum(<span style="color: black;">101</span>))<span style="color: black;"># 50050</span>
    print(mysum(<span style="color: black;">1</span>, <span style="color: black;">100</span>)) <span style="color: black;"># 4950</span>
    print(mysum(<span style="color: black;">0</span>, <span style="color: black;">101</span>, <span style="color: black;">2</span>) ) <span style="color: black;"># 2550</span>

    <span style="color: black;"># <span style="color: black;">办法</span>二:</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">mysum</span><span style="color: black;">(*args)</span>:</span>
    <span style="color: black;">return</span> sum(range(*args))


    print(mysum(<span style="color: black;">101</span>)) <span style="color: black;"># 5050</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">9.创建一个列表L = []写一个函数 input_number读取数据放入列表L中</h1><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">input_number</span><span style="color: black;">()</span>:</span>
    L = list()
    <span style="color: black;">while</span> <span style="color: black;">True</span>:
    s = input(<span style="color: black;">"输入-1结束"</span>) <span style="color: black;">or</span> <span style="color: black;">"-1"</span>
    <span style="color: black;">if</span> s == <span style="color: black;">"-1"</span>:
    <span style="color: black;">break</span>
    L.append(s)
    <span style="color: black;">return</span>L


    print(input_number())<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/dfic-imagehandler/816e3aa8-043c-47c0-988c-b39279717f1a~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723899749&amp;x-signature=MqnQ9QLfoWboIKlDxF5icZt%2Bhfc%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">10.写一个函数primes(n),返回<span style="color: black;">少于</span>n的所有的素数的列表</h1><span style="color: black;">def</span> <span style="color: black;">primes(n):</span>
    <span style="color: black;">if</span> <span style="color: black;">n &lt; 2:</span>
    <span style="color: black;">return</span> <span style="color: black;">[]</span>
    <span style="color: black;">L</span> = <span style="color: black;"></span>
    <span style="color: black;">for</span> <span style="color: black;">i in range(n): # 循环判断2-n之间的数是不是素数</span>
    <span style="color: black;">for</span> <span style="color: black;">j in range(2, i): # 判断i是不是素数</span>
    <span style="color: black;">if</span> <span style="color: black;">i % j == 0:</span>
    <span style="color: black;">break</span>
    <span style="color: black;">if</span> <span style="color: black;">i - j == 1:</span>
    <span style="color: black;">L.append(i)</span>
    <span style="color: black;">return</span> <span style="color: black;">L</span>


    <span style="color: black;">print(primes(10))</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">11.写一个lambda表达式,判断这个数的2次方+1能否被5整数,<span style="color: black;">倘若</span>能整除返回True,否则返回False</h1><span style="color: black;">fx</span> <span style="color: black;">=</span> <span style="color: black;">lambda x:</span> <span style="color: black;">True</span> <span style="color: black;">if</span> <span style="color: black;">(x**2</span> <span style="color: black;">+</span> <span style="color: black;">1</span><span style="color: black;">)</span> <span style="color: black;">%</span> <span style="color: black;">5</span> <span style="color: black;">==</span> <span style="color: black;">0</span> <span style="color: black;">else</span> <span style="color: black;">False</span>
    <span style="color: black;">print(fx(3))</span> <span style="color: black;"># True</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">12.写一个lambda 表达式,求两个变量的最大值</h1>fx = <span style="color: black;">lambda</span> x, y: x <span style="color: black;">if</span> x &gt;y <span style="color: black;">else</span> y
    print(fx(<span style="color: black;">4</span>, <span style="color: black;">3</span>)) <span style="color: black;"># 4</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">13.自己写一个程序,解释执行用户输入的任何语句</h1>g = {}
    l = {}
    <span style="color: black;">while</span> <span style="color: black;">True</span>:
    s = input(<span style="color: black;">"&gt;&gt;&gt;"</span>) <span style="color: black;">or</span> <span style="color: black;">"-1"</span>
    <span style="color: black;">if</span> s == <span style="color: black;">"-1"</span>:
    <span style="color: black;">break</span>
    exec(s, g, l)

    print(g)
    print(l)<h1 style="color: black; text-align: left; margin-bottom: 10px;">14.求: 1 + 1/2 + 1/4 + 1/8 + ... + 1/2**n 的和</h1># <span style="color: black;">办法</span>一
    n = <span style="color: black;">int</span>(input(<span style="color: black;">"&gt;&gt;&gt;"</span>) or <span style="color: black;">-1</span>)
    <span style="color: black;">print</span>(sum([<span style="color: black;">1</span>/<span style="color: black;">2</span>**n <span style="color: black;">for</span> x in <span style="color: black;">range</span>(n + <span style="color: black;">1</span>)]))
    # <span style="color: black;">办法</span>二
    res = sum(<span style="color: black;">map</span>(lambda x: <span style="color: black;">1</span>/<span style="color: black;">2</span>**x, <span style="color: black;">range</span>(<span style="color: black;">int</span>(input(<span style="color: black;">"&gt;&gt;&gt;"</span>)))))
    <span style="color: black;">print</span>(res)<h1 style="color: black; text-align: left; margin-bottom: 10px;">15.求 1**2 + 2**2 + 3**2 + ... 9**2 的和</h1>res = sum(<span style="color: black;">map</span>(lambda x: x**<span style="color: black;">2</span>, <span style="color: black;">range</span>(<span style="color: black;">10</span>)))
    <span style="color: black;">print</span>(res)<h1 style="color: black; text-align: left; margin-bottom: 10px;">16.求 1**9 + 2**8 + 3**7 + ... 9**1 的和</h1><span style="color: black;"># <span style="color: black;">办法</span>一</span>
    <span style="color: black;">print(sum(map(lambda</span> <span style="color: black;">x,</span> <span style="color: black;">y:</span> <span style="color: black;">x**y,</span> <span style="color: black;">range(1,</span> <span style="color: black;">10</span><span style="color: black;">),</span> <span style="color: black;">range(9,</span> <span style="color: black;">0</span><span style="color: black;">,</span> <span style="color: black;">-1</span><span style="color: black;">))))</span>
    <span style="color: black;"># <span style="color: black;">办法</span>二</span>
    <span style="color: black;">print(sum(map(pow,</span> <span style="color: black;">range(1,</span> <span style="color: black;">10</span><span style="color: black;">)),</span> <span style="color: black;">range(9,</span> <span style="color: black;">0</span><span style="color: black;">,</span> <span style="color: black;">-1</span><span style="color: black;">)))</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">17.编写函数 myfac(x) 计算x的阶乘 x! 例如:5! = 5 * 4 * 3 * 2 * 1</h1><span style="color: black;"># 匿名函数和递归嵌套运用</span>
    <span style="color: black;">fx</span> <span style="color: black;">=</span> <span style="color: black;">lambda x:</span> <span style="color: black;">1</span> <span style="color: black;">if</span> <span style="color: black;">x</span> <span style="color: black;">==</span> <span style="color: black;">1</span> <span style="color: black;">else</span> <span style="color: black;">x</span> <span style="color: black;">*</span> <span style="color: black;">fx(x</span> <span style="color: black;">-</span> <span style="color: black;">1</span><span style="color: black;">)</span>
    <span style="color: black;">print(fx(5))</span> <span style="color: black;"># 120</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">18.写程序算出1~20的阶乘的和 1! + 2! + 3! + .... + 19! + 20!</h1><span style="color: black;"># 匿名函数和递归嵌套运用</span>
    <span style="color: black;">fx</span> <span style="color: black;">=</span> <span style="color: black;">lambda x:</span> <span style="color: black;">1</span> <span style="color: black;">if</span> <span style="color: black;">x</span> <span style="color: black;">==</span> <span style="color: black;">1</span> <span style="color: black;">else</span> <span style="color: black;">x*fx(x</span> <span style="color: black;">-</span> <span style="color: black;">1</span><span style="color: black;">)</span>
    <span style="color: black;">print(sum(map(fx,</span> <span style="color: black;">range(1,</span> <span style="color: black;">21</span><span style="color: black;">))))</span> <span style="color: black;"># 2561327494111820313</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">19.已知有列表:L = [, 10, [, 15], 18]</h1><span style="color: black;"># 1) 写一个函数 print_list(lst) 打印出列表中所有数字</span>
    <span style="color: black;">L</span> <span style="color: black;">=</span> <span style="color: black;">[,</span> <span style="color: black;">10</span><span style="color: black;">,</span> <span style="color: black;">[,</span> <span style="color: black;">15</span><span style="color: black;">],</span> <span style="color: black;">18</span><span style="color: black;">]</span>


    <span style="color: black;">def</span> <span style="color: black;">print_list(lst):</span>
    <span style="color: black;">for i in lst:</span>
    <span style="color: black;">if</span> <span style="color: black;">type(i)</span> <span style="color: black;">is list:</span>
    <span style="color: black;">print_list(i)</span>
    <span style="color: black;">else:</span>
    <span style="color: black;">print(i)</span>


    <span style="color: black;">print_list(L)</span>
    <span style="color: black;"># 2) 写一个函数sum_list(lst) 返回列表中所有数字的和</span>
    <span style="color: black;">L</span> <span style="color: black;">=</span> <span style="color: black;">[,</span> <span style="color: black;">10</span><span style="color: black;">,</span> <span style="color: black;">[,</span> <span style="color: black;">15</span><span style="color: black;">],</span> <span style="color: black;">18</span><span style="color: black;">]</span>


    <span style="color: black;">def</span> <span style="color: black;">sum_list(lst):</span>
    <span style="color: black;">s</span> <span style="color: black;">=</span> <span style="color: black;">0</span>
    <span style="color: black;">for i in lst:</span>
    <span style="color: black;">if</span> <span style="color: black;">type(i)</span> <span style="color: black;">is list:</span>
    <span style="color: black;">s</span> <span style="color: black;">+=</span> <span style="color: black;">sum_list(i)</span>
    <span style="color: black;">else:</span>
    <span style="color: black;">s</span> <span style="color: black;">+=</span> <span style="color: black;">i</span>
    <span style="color: black;">return</span> <span style="color: black;">s</span>


    <span style="color: black;">print(sum_list(L))</span> <span style="color: black;"># 86</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">20.输入一个圆的半径打印出这个圆的面积,输入一个圆的面积打印出这个圆的半径,用math模块实现</h1>import <span style="color: black;">math</span>


    <span style="color: black;">print</span>(<span style="color: black;">math</span>.<span style="color: black;">pi</span> * <span style="color: black;">math</span>.<span style="color: black;">pow</span>(int(<span style="color: black;">input</span>(<span style="color: black;">"r:"</span>)), <span style="color: black;">2</span>))
    <span style="color: black;">print</span>(<span style="color: black;">math</span>.<span style="color: black;">sqrt</span>(int(<span style="color: black;">input</span>(<span style="color: black;">"area:"</span>))/<span style="color: black;">math</span>.<span style="color: black;">pi</span>)) # <span style="color: black;">math</span>.<span style="color: black;">sqrt</span> 求平方根<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/dfic-imagehandler/e380db65-99dc-4b76-a3e0-32c960a1f19b~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723899749&amp;x-signature=Zg80DOJAyDVqjjTm5DleQLt%2FlwI%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">21.请编写函数 fun(n) 其功能是计算和 Sn = 1 + 1/1! + 1/2! + 1/3! + 1/4! +... +1/n!</h1><span style="color: black;">import</span> math


    <span style="color: black;"># <span style="color: black;">办法</span>一</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">fun</span><span style="color: black;">(n)</span>:</span>
    s = <span style="color: black;">0</span>
    <span style="color: black;">for</span> i <span style="color: black;">in</span> range(n+<span style="color: black;">1</span>):
    s += <span style="color: black;">1</span>/math.factorial(i) <span style="color: black;"># math.factorial(i) 求i的阶乘 i!</span>
    print(s)


    fun(<span style="color: black;">10</span>) <span style="color: black;"># 2.7182818011463845</span>


    <span style="color: black;"># <span style="color: black;">办法</span>二</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">func</span><span style="color: black;">(n)</span>:</span>
    <span style="color: black;">return</span> sum(map(<span style="color: black;">lambda</span> i: <span style="color: black;">1</span>/math.factorial(i), range(n+<span style="color: black;">1</span>)))


    print(func(<span style="color: black;">10</span>)) <span style="color: black;"># 2.7182818011463845</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">22.编写函数 fun(x, n) 计算s = 1 + x + x**2/2! + x**3/3! + x**n/n!的和并返回</h1><span style="color: black;">import</span> math


    def <span style="color: black;"><span style="color: black;">fun</span><span style="color: black;">(x, n)</span></span>:
    <span style="color: black;">return</span>sum(map(lambda n: math.pow(x, n)/math.factorial(n), range(n)))


    print(<span style="color: black;"><span style="color: black;">fun</span><span style="color: black;">(<span style="color: black;">3.1</span>, <span style="color: black;">10</span>)</span></span>) # <span style="color: black;">22.1668536447133</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">23.写一个程序,以电子时钟的格式<span style="color: black;">表示</span>时间:HH:MM:SS</h1>import <span style="color: black;">time</span>


    def show_time():
    <span style="color: black;">while</span> True:
    t = <span style="color: black;">localtime</span>() <span style="color: black;"># 年 月 日 时 分 秒 星期 一年中的第几天 夏令时</span>
    <span style="color: black;">s</span> = <span style="color: black;">"\r%02d:%02d:%02d"</span> % t[<span style="color: black;">3</span>:<span style="color: black;">6</span>] <span style="color: black;"># \r 回车后的光标位置</span>
    <span style="color: black;">print</span>(<span style="color: black;">s</span>, end=)
    time.sleep(<span style="color: black;">1</span>)


    show_time()<h1 style="color: black; text-align: left; margin-bottom: 10px;">24.输入你的生日,计算出你出生的那天是星期几?<span style="color: black;">已然</span>出生了多少天?</h1>import <span style="color: black;">time</span>


    y = int(<span style="color: black;">input</span>(<span style="color: black;">"输入出生的年:"</span>))
    m = int(<span style="color: black;">input</span>(<span style="color: black;">"输入出生的月:"</span>))
    d = int(<span style="color: black;">input</span>(<span style="color: black;">"输入出生的日:"</span>))

    t = <span style="color: black;">time</span>.mktime((y, m, d, <span style="color: black;">0</span>, <span style="color: black;">0</span>, <span style="color: black;">0</span>, <span style="color: black;">0</span>, <span style="color: black;">0</span>, <span style="color: black;">0</span>))
    <span style="color: black;">print</span>(<span style="color: black;">"UTC时间的秒数:"</span>, t)

    time_tuple = <span style="color: black;">time</span>.localtime(t)
    week = time_tuple[<span style="color: black;">6</span>]
    L = [<span style="color: black;">星期一</span>,
    <span style="color: black;">星期二</span>,
    <span style="color: black;">星期三</span>,
    <span style="color: black;">星期四</span>,
    <span style="color: black;">星期五</span>,
    <span style="color: black;">星期六</span>,
    <span style="color: black;">星期日</span>]

    <span style="color: black;">print</span>(<span style="color: black;">"这一天是:"</span>, L)

    t = <span style="color: black;">time</span>.<span style="color: black;">time</span>() - t # 从生日到<span style="color: black;">此刻</span>过了多少秒
    d = t / (<span style="color: black;">60</span> * <span style="color: black;">60</span> * <span style="color: black;">24</span>) # t / 一天的秒数

    <span style="color: black;">print</span>(<span style="color: black;">"您已出生"</span>, d, <span style="color: black;">"天"</span>)<h1 style="color: black; text-align: left; margin-bottom: 10px;">25.<span style="color: black;">能够</span><span style="color: black;">做为</span><span style="color: black;">秘码</span>的字符有:<span style="color: black;">体积</span>写的A-Z,0_9,下划线,写一个程序随机生成六位<span style="color: black;">秘码</span></h1>import random


    def get_random_passwd(n):
    source = [<span style="color: black;">chr</span>(<span style="color: black;">x</span>) <span style="color: black;">for</span> <span style="color: black;">x</span> in range(<span style="color: black;">ord</span>(<span style="color: black;">"a"</span>), <span style="color: black;">ord</span>(<span style="color: black;">"z"</span>)+<span style="color: black;">1</span>)]
    source += [<span style="color: black;">chr</span>(<span style="color: black;">x</span>) <span style="color: black;">for</span> <span style="color: black;">x</span> in range(<span style="color: black;">ord</span>(<span style="color: black;">"A"</span>), <span style="color: black;">ord</span>(<span style="color: black;">"Z"</span>)+<span style="color: black;">1</span>)]
    source +=
    code = <span style="color: black;">""</span>
    <span style="color: black;">while</span> len(code) &lt; n:
    code += random.choice(source)
    <span style="color: black;">return</span> code


    <span style="color: black;">print</span>(get_random_passwd(<span style="color: black;">6</span>))<h1 style="color: black; text-align: left; margin-bottom: 10px;">26.编写一个闹钟程序,<span style="color: black;">起步</span>时设置<span style="color: black;">按时</span>时间(小时和分钟),到时间后打印"时间到....." <span style="color: black;">而后</span>退出程序</h1>import <span style="color: black;">time</span>def<span style="color: black;">alarm</span>():
    h = <span style="color: black;">int</span>(input(<span style="color: black;">"请输入小时: "</span>))
    <span style="color: black;">m</span> = <span style="color: black;">int</span>(input(<span style="color: black;">"请输入分钟: "</span>))
    <span style="color: black;">while</span> True:
    ct = time.localtime() <span style="color: black;"># 当前时间的元组</span>
    <span style="color: black;">if</span> (h, <span style="color: black;">m</span>) == ct[<span style="color: black;">3</span>:<span style="color: black;">5</span>]:
    <span style="color: black;">print</span>(<span style="color: black;">"\n时间到..."</span>)
    <span style="color: black;">return</span>
    <span style="color: black;">print</span>(<span style="color: black;">"\r%02d:%02d:%02d"</span> % ct[<span style="color: black;">3</span>:<span style="color: black;">6</span>], end=)
    time.sleep(<span style="color: black;">1</span>)


    <span style="color: black;"># <span style="color: black;">起步</span>闹钟</span>
    <span style="color: black;">alarm</span>()<h1 style="color: black; text-align: left; margin-bottom: 10px;">27.模拟斗地主发牌,扑克牌共54张,三个人,每人发17张牌,底牌留三张</h1><span style="color: black;">花色:黑桃(\u2660), 梅花(\u2663), 方块(\u2665), 红桃(\u2666)
      数值:A2-10JQK <span style="color: black;">体积</span>王
      输入回车,打印第1个人的17张牌,输入回车,打印第2个人的17张牌
      输入回车,打印第3个人的17张牌,再输入回车,打印出三张底牌</span>
    <span style="color: black;">import</span> random


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">get_new_poker</span><span style="color: black;">()</span>:</span>
    kinds = [<span style="color: black;">\u2660</span>, <span style="color: black;">\u2663</span>, <span style="color: black;">\u2665</span>, <span style="color: black;">\u2666</span>]

    numbers = [<span style="color: black;">A</span>]
    numbers +=
    numbers += [<span style="color: black;">J</span>, <span style="color: black;">Q</span>, <span style="color: black;">K</span>]
    <span style="color: black;"># 以下生成52张牌</span>
    L =
    L += [<span style="color: black;">大王</span>, <span style="color: black;">小王</span>]
    <span style="color: black;">return</span> L


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">play</span><span style="color: black;">()</span>:</span>
    poker = get_new_poker()
    random.shuffle(poker)
    input(<span style="color: black;">"按任意键发<span style="color: black;">第1</span>个人的牌: "</span>)
    print(<span style="color: black;">"<span style="color: black;">第1</span>个人的牌是:"</span>, poker[:<span style="color: black;">17</span>])
    input(<span style="color: black;">"按任意键发第二个人的牌: "</span>)
    print(<span style="color: black;">"第二个人的牌是:"</span>, poker[<span style="color: black;">17</span>:<span style="color: black;">34</span>])
    input(<span style="color: black;">"按任意键发第三个人的牌: "</span>)
    print(<span style="color: black;">"第三个人的牌是:"</span>, poker[<span style="color: black;">34</span>:<span style="color: black;">51</span>])
    input(<span style="color: black;">"按任意键看底牌: "</span>)
    print(<span style="color: black;">"底牌是:"</span>, poker[<span style="color: black;">51</span>:])


    play()<h1 style="color: black; text-align: left; margin-bottom: 10px;">28.写函数get_score来获取输入的学生成绩(0~100的整数),<span style="color: black;">显现</span>错误则返回0,输入正确返回这个数</h1><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">get_score</span><span style="color: black;">()</span>:</span>
    <span style="color: black;">try</span>:
    score = int(input(<span style="color: black;">"请输入成绩:"</span>))
    <span style="color: black;">except</span> ValueError:
    <span style="color: black;">return</span> <span style="color: black;">0</span>
    <span style="color: black;">if</span> score <span style="color: black;">in</span> range(<span style="color: black;">101</span>):
    <span style="color: black;">return</span> score
    <span style="color: black;">return</span> <span style="color: black;">0</span>


    score = get_score()
    print(<span style="color: black;">学生的成绩是:</span>, score)<h1 style="color: black; text-align: left; margin-bottom: 10px;">29.一个球从100米高度落下,每次落地后反弹高度为原高度的一半,再落下</h1><span style="color: black;">1) 写程序算出皮球从第10次落地后反弹高度是多少?
      2) 球共经<span style="color: black;">太多</span>少米路径?</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">fall_height</span><span style="color: black;">(m, n, s=<span style="color: black;">0</span>)</span>:</span>
    s += m
    <span style="color: black;">if</span> n == <span style="color: black;">0</span>:
    <span style="color: black;">return</span> m, s
    <span style="color: black;">return</span> fall_height(m / <span style="color: black;">2</span>, n - <span style="color: black;">1</span>, s)


    ret = fall_height(<span style="color: black;">100</span>, <span style="color: black;">10</span>)
    print(<span style="color: black;">"最后落地后反弹高度是%s 球共经过%s米"</span> % ret)<h1 style="color: black; text-align: left; margin-bottom: 10px;">30.输入一个正整数,分解质因数,如90 = 2*3*3*5, (质因数<span style="color: black;">指的是</span>最小能被原数整除的素数(不<span style="color: black;">包括</span>1))</h1><span style="color: black;">def</span> <span style="color: black;">primefactor(n, lst, pf_lst):</span>
    <span style="color: black;">for</span> <span style="color: black;">i in lst:</span>
    <span style="color: black;">if</span> <span style="color: black;">n % i == 0:</span>
    <span style="color: black;">pf_lst.append(i)</span>
    <span style="color: black;"> # 递归加入分解得到的质因数</span>
    <span style="color: black;">return</span> <span style="color: black;">primefactor(n / i, lst, pf_lst)</span>


    <span style="color: black;">def</span> <span style="color: black;">qualty(n):</span>
    <span style="color: black;">qualty_lst</span> = <span style="color: black;"></span>
    <span style="color: black;"> # 拿到<span style="color: black;">少于</span>n的素数列表 qualty_lst</span>
    <span style="color: black;">for</span> <span style="color: black;">i in range(2, n//4+1): # 最小分解质因数是4= 2*2, 优化算法n//4+1</span>
    <span style="color: black;">for</span> <span style="color: black;">j in range(2, i):</span>
    <span style="color: black;">if</span> <span style="color: black;">i % j == 0:</span>
    <span style="color: black;">break</span>
    <span style="color: black;">if</span> <span style="color: black;">i - j == 1:</span>
    <span style="color: black;">qualty_lst.append(i)</span>
    <span style="color: black;"> # 循环加入90//2 +1 以内的质素</span>
    <span style="color: black;"># </span>
    <span style="color: black;">pf_lst</span> = <span style="color: black;">[]</span>
    <span style="color: black;"> # 拿到<span style="color: black;">少于</span>n的分解质因数递归返回列表 pf_lst</span>
    <span style="color: black;">primefactor(n,</span> <span style="color: black;">qualty_lst, pf_lst)</span>
    <span style="color: black;">s</span> = <span style="color: black;">""</span>
    <span style="color: black;"> # 格式化输出</span>
    <span style="color: black;">for</span> <span style="color: black;">i in pf_lst:</span>
    <span style="color: black;">s</span> <span style="color: black;">+= str(i)</span>
    <span style="color: black;">s</span> = <span style="color: black;">"%s = " % n + "*".join(s)</span>
    <span style="color: black;">print(s)</span>


    <span style="color: black;">qualty(90)</span>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/dfic-imagehandler/4d3b2a2d-ab05-476e-b665-c068997c504c~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723899749&amp;x-signature=mG5wPLRqBkVDyPxa8a7TdIt0dD0%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">31.已知s = {"工商银行", "建设银行", "中国银行", "农业银行"}</h1><span style="color: black;"># 1.用for 语句遍历集合中的元素并打印</span>
    <span style="color: black;"># 2.将上面的for 语句改写为while语句实现上面<span style="color: black;">一样</span>的功能</span>
    s = {<span style="color: black;">"工商银行"</span>, <span style="color: black;">"建设银行"</span>, <span style="color: black;">"中国银行"</span>, <span style="color: black;">"农业银行"</span>}
    <span style="color: black;">for</span> i <span style="color: black;">in</span>s:
    print(i)
    t = iter(s)<span style="color: black;">try</span>:
    <span style="color: black;">while</span> <span style="color: black;">True</span>:
    print(next(t))
    <span style="color: black;">except</span> Exception:
    <span style="color: black;">pass</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">32.写入一个生成器函数, myodd(start, stop) 用于生成start<span style="color: black;">起始</span>到stop结束(不<span style="color: black;">包括</span>stop)的所有奇数</h1>def myodd(<span style="color: black;">start</span>, <span style="color: black;">stop</span>):
    <span style="color: black;">while</span> <span style="color: black;">start</span> &lt; <span style="color: black;">stop</span>:
    <span style="color: black;">if</span> <span style="color: black;">start</span> % <span style="color: black;">2</span> != <span style="color: black;">0</span>:
    yield <span style="color: black;">start</span>
    <span style="color: black;">if</span> <span style="color: black;">start</span> &lt; <span style="color: black;">stop</span>:
    <span style="color: black;">start</span> += <span style="color: black;">1</span>


    <span style="color: black;">for</span> i <span style="color: black;">in</span> myodd(<span style="color: black;">1</span>, <span style="color: black;">100</span>):
    print(i)
    L =
    print(L)<h1 style="color: black; text-align: left; margin-bottom: 10px;">33.写一个程序,读入任意行的文字数据,当输入空行时结束输入,打印带有行号的输入结果</h1>L = []
    <span style="color: black;">while</span> True:
    <span style="color: black;">s</span> = input(<span style="color: black;">"请输入: "</span>)
    <span style="color: black;">if</span> <span style="color: black;">not</span> <span style="color: black;">s</span>:
    <span style="color: black;">break</span>
    L.append(<span style="color: black;">s</span>)
    <span style="color: black;">for</span> t in enumerate(L, <span style="color: black;">1</span>): <span style="color: black;"># t= (0, "hello")</span>
    <span style="color: black;">print</span>(<span style="color: black;">"第%d行: %s"</span> % t)<h1 style="color: black; text-align: left; margin-bottom: 10px;">34.用生成器函数生成斐波那契数列的前n个数:1 1 2 3 5 8 13 ... n</h1><span style="color: black;"># 1) 输出前 20 个数</span>
    <span style="color: black;"># 2) 求前 30 个数的和</span>
    <span style="color: black;">def</span> <span style="color: black;">fibonacci(n):</span>
    <span style="color: black;">a</span> = <span style="color: black;">0</span>
    <span style="color: black;">b</span> = <span style="color: black;">1</span>
    <span style="color: black;">for</span> <span style="color: black;">i in range(1, n+1):</span>
    <span style="color: black;">a,</span> <span style="color: black;">b = b, a + b</span>
    <span style="color: black;">yield</span> <span style="color: black;">a</span>


    <span style="color: black;">for</span> <span style="color: black;">i in fibonacci(20):</span>
    <span style="color: black;">print(i)</span>
    <span style="color: black;">print(sum(fibonacci(30)))</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">35.写程序打印杨辉三角(只打印6层)</h1>
    <span style="color: black;">1</span> <span style="color: black;">0+1</span>
    <span style="color: black;">1</span> <span style="color: black;">1 0+1 1+0</span>
    <span style="color: black;">1</span> <span style="color: black;">2 1 0+1 1+1 1+0</span>
    <span style="color: black;">1</span> <span style="color: black;">3 3 1 0+1 1+2 2+1 1+0</span>
    <span style="color: black;">1</span> <span style="color: black;">4 6 4 1</span>
    <span style="color: black;">1</span> <span style="color: black;">5 10 10 5 1</span>

    <span style="color: black;">def</span> <span style="color: black;">triangle(n):</span>
    <span style="color: black;">L</span> = <span style="color: black;">[]</span>
    <span style="color: black;">if</span> <span style="color: black;">not L:</span>
    <span style="color: black;">L.append(1)</span>
    <span style="color: black;">yield</span> <span style="color: black;">L</span>
    <span style="color: black;">for</span> <span style="color: black;">i in range(n-1):</span>
    <span style="color: black;">R</span> = <span style="color: black;">[]</span>
    <span style="color: black;">for</span> <span style="color: black;">i in range(len(L)-1):</span>
    <span style="color: black;">R.append(L+L)</span>
    <span style="color: black;">L</span> = <span style="color: black;"> + R + </span>
    <span style="color: black;">yield</span> <span style="color: black;">L</span>


    <span style="color: black;">for</span> <span style="color: black;">i in triangle(6):</span>
    <span style="color: black;">s</span>=<span style="color: black;">for</span> <span style="color: black;">j in i:</span>
    <span style="color: black;">s</span> <span style="color: black;">+= str(j) + </span>
    <span style="color: black;">print(s.center(13))</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">36.将如下数据用文本编辑器写入到data.txt文件中</h1><span style="color: black;">"""
      小张 13888888888
      小李 13999999999
      小赵 13666666666
      写程序读取数据,打印出姓名和<span style="color: black;">tel</span>号码,格式如 姓名: 小张, <span style="color: black;">tel</span>: 13888888888
      """</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">read_data</span><span style="color: black;">()</span>:</span>
    <span style="color: black;">try</span>:
    f = open(<span style="color: black;">"data.txt"</span>)
    L = []
    <span style="color: black;">while</span> <span style="color: black;">True</span>:
    s = f.readline()
    <span style="color: black;">if</span> <span style="color: black;">not</span>s:
    f.close()<span style="color: black;">return</span> L
    s = s.rstrip()
    name, number = s.split(<span style="color: black;">" "</span>)
    L.append((name, number))
    <span style="color: black;">except</span> IOError:
    print(<span style="color: black;">"打开文件失败"</span>)


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">read_lst</span><span style="color: black;">(L)</span>:</span>
    <span style="color: black;">for</span> line <span style="color: black;">in</span> L:
    print(<span style="color: black;">"姓名:%s, <span style="color: black;">tel</span>:%s"</span> % line)


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">main</span><span style="color: black;">()</span>:</span>
    L = read_data()
    read_lst(L)


    main()<h1 style="color: black; text-align: left; margin-bottom: 10px;">37.读入任意行的文字信息,当输入空行时结束输入,将读入的字符串存于列表写入文件 input.txt 中</h1><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">read_input</span><span style="color: black;">()</span>:</span>
    L = []
    <span style="color: black;">while</span> <span style="color: black;">True</span>:
    s = input(<span style="color: black;">"请输入: "</span>)
    <span style="color: black;">if</span> <span style="color: black;">not</span> s:
    <span style="color: black;">break</span>
    L.append(s)
    <span style="color: black;">return</span> L


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">write_input</span><span style="color: black;">(L)</span>:</span>
    <span style="color: black;">try</span>:
    <span style="color: black;"># 打开文件</span>
    f = open(<span style="color: black;">"input.txt"</span>, <span style="color: black;">w</span>)
    <span style="color: black;"># 写入数据</span>
    <span style="color: black;">for</span> line <span style="color: black;">in</span> L:
    f.write(line)
    f.write(<span style="color: black;">\n</span>) <span style="color: black;"># 写入换行符(\n, \r\n)</span>
    <span style="color: black;"># 关闭文件</span>
    f.close()
    <span style="color: black;">except</span> IOError:
    print(<span style="color: black;">"写文件失败"</span>)


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">main</span><span style="color: black;">()</span>:</span>lst = read_input()
    print(lst)
    write_input(lst)


    main()<h1 style="color: black; text-align: left; margin-bottom: 10px;">38.从input.txt中读取之前输入的数据存入列表中,再加上行号进行打印<span style="color: black;">表示</span>,如 第1行: aaaa</h1><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">read_input_file</span><span style="color: black;">(filename=<span style="color: black;">input.txt</span>)</span>:</span>
    f = open(filename)
    L = []
    <span style="color: black;">while</span> <span style="color: black;">True</span>:
    <span style="color: black;"># 读取每一行数据</span>
    s = f.readline()
    <span style="color: black;">if</span> <span style="color: black;">not</span> s:
    <span style="color: black;">break</span>
    <span style="color: black;"># 把换行符\n去掉</span>
    s = s.rstrip()
    L.append(s) <span style="color: black;"># 放入列表</span>
    f.close() <span style="color: black;"># 关闭文件</span>
    <span style="color: black;"># 返回L绑定的列表</span>
    <span style="color: black;">return</span> L


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">print_file_info</span><span style="color: black;">(L)</span>:</span>
    <span style="color: black;">for</span>line<span style="color: black;">in</span> enumerate(L, <span style="color: black;">1</span>):
    print(<span style="color: black;">"第%d行: %s"</span> % line)


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">main</span><span style="color: black;">()</span>:</span>
    lst = read_input_file()
    print(lst)
    print_file_info(lst)


    main()<h1 style="color: black; text-align: left; margin-bottom: 10px;">39.用装饰器实现用户验证</h1>user_list = [
    {<span style="color: black;">"name"</span>: <span style="color: black;">"coco"</span>, <span style="color: black;">"passwd"</span>: <span style="color: black;">123</span>},
    {<span style="color: black;">"name"</span>: <span style="color: black;">"zhangsan"</span>, <span style="color: black;">"passwd"</span>: <span style="color: black;">123</span>},
    {<span style="color: black;">"name"</span>: <span style="color: black;">"lisi"</span>, <span style="color: black;">"passwd"</span>: <span style="color: black;">123</span>},
    {<span style="color: black;">"name"</span>: <span style="color: black;">"wangwu"</span>, <span style="color: black;">"passwd"</span>: <span style="color: black;">123</span>}
    ] <span style="color: black;"># 定义一个列表存储用户数据源</span>
    current_dic = {<span style="color: black;">"username"</span>: <span style="color: black;">None</span>, <span style="color: black;">"login"</span>: <span style="color: black;">False</span>} <span style="color: black;"># 记录用户登录状态</span>

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">auth</span><span style="color: black;">(auth_type=<span style="color: black;">"MySQL.db"</span>)</span>:</span> <span style="color: black;"># 留给用户传递数据<span style="color: black;">源自</span></span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">auth_func</span><span style="color: black;">(func)</span>:</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">wrapper</span><span style="color: black;">(*args, **kwargs)</span>:</span>
    print(<span style="color: black;">"数据库<span style="color: black;">源自</span>是: %s"</span> % auth_type)
    <span style="color: black;">if</span> auth_type == user_list:
    <span style="color: black;">if</span> current_dic[<span style="color: black;">"username"</span>] <span style="color: black;">and</span> current_dic[<span style="color: black;">"login"</span>]:
    res = func(*args, **kwargs)<span style="color: black;">return</span> res
    username = input(<span style="color: black;">"请输入账号:"</span>).strip()
    passwd = int(input(<span style="color: black;">"请输入<span style="color: black;">秘码</span>:"</span>).strip())
    <span style="color: black;">for</span> user_dic <span style="color: black;">in</span> user_list:
    <span style="color: black;">if</span> username == user_dic[<span style="color: black;">"name"</span>] <span style="color: black;">and</span>passwd ==user_dic[<span style="color: black;">"passwd"</span>]:
    current_dic[<span style="color: black;">"username"</span>] = username
    current_dic[<span style="color: black;">"login"</span>] = <span style="color: black;">True</span>
    ret = func(*args, **kwargs)
    <span style="color: black;">return</span> ret
    <span style="color: black;">else</span>:
    print(<span style="color: black;">"账号或<span style="color: black;">秘码</span>不正确"</span>)
    <span style="color: black;">else</span>:
    print(<span style="color: black;">"请指定源数据库"</span>)

    <span style="color: black;">return</span> wrapper

    <span style="color: black;">return</span> auth_func


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">index</span><span style="color: black;">()</span>:</span>
    print(<span style="color: black;">"欢迎来到主页"</span>)


    <span style="color: black;">@auth(auth_type=user_list)</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">home</span><span style="color: black;">(name)</span>:</span>
    print(<span style="color: black;">"欢迎回家%s"</span> %name)


    <span style="color: black;">@auth()</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">shopping</span><span style="color: black;">(name)</span>:</span>
    print(<span style="color: black;">"%s的购物车里有[手机,外星人,Mac]"</span> %name)


    index()
    home(<span style="color: black;">"coco"</span>)
    shopping(<span style="color: black;">"coco"</span>)<h1 style="color: black; text-align: left; margin-bottom: 10px;">49.学生信息管理项目</h1><span style="color: black;"><span style="color: black;">必须</span>两个函数:
      input_student() 返回学生信息的字典的列表
      output_student(lst) 以表格式方打印学生信息
      学生信息:
      姓名(name) 字符串
      年龄(age) 整数
      成绩(score) 整数
      input_student()调用时等待用户输入学生信息,当输入姓名为空时结束输入形成字典后存如列表中返回
      L = input_student()
      请输入学生姓名: xiaozhang
      请输入学生年龄: 20
      请输入学生成绩: 98
      请输入学生姓名: xiaoli
      请输入学生年龄: 23
      请输入学生成绩: 100
      请输入学生姓名: &lt;回车&gt;输入结束
      此时列表数据为:
      L = [{</span>name<span style="color: black;">:</span>xiaozhang<span style="color: black;">,
    </span>age<span style="color: black;">:20,
    </span>score<span style="color: black;">: 98},
      {</span>name<span style="color: black;">:</span>xiaoli<span style="color: black;">,
    </span>age<span style="color: black;">:23,
    </span>score<span style="color: black;">: 100}
      ]
      def output_student(lst):pass
      print(L) # 打印出字典的列表
      output_student(L) # 打印出学生信息的表格如下:
      +-----------+--------+---------+
      | name | age | score |
      +-----------+--------+---------+
      | xiaozhang | 20 | 98 |
      | xiaoli | 23 | 100 |
      +-----------+--------+---------+</span>
    def input_student():
    L = []
    <span style="color: black;">while</span> True:
    name = input(<span style="color: black;">"请输入学生姓名:"</span>) <span style="color: black;">or</span> <span style="color: black;">-1</span>
    <span style="color: black;">if</span> name == <span style="color: black;">-1</span>:
    <span style="color: black;">break</span>
    age = <span style="color: black;">int</span>(input(<span style="color: black;">"请输入学生年龄:"</span>))
    score = <span style="color: black;">int</span>(input(<span style="color: black;">"请输入学生成绩:"</span>))
    L.append({<span style="color: black;">name</span>: name,
    <span style="color: black;">age</span>: age,
    <span style="color: black;">score</span>: score
    })
    <span style="color: black;">return</span> L


    def head(<span style="color: black;">length</span>):
    <span style="color: black;">s</span> = <span style="color: black;">+%s+%s+%s+</span> % (<span style="color: black;">-</span> * (<span style="color: black;">length</span>), <span style="color: black;">-</span>*<span style="color: black;">5</span>, <span style="color: black;">-</span>*<span style="color: black;">7</span>)
    <span style="color: black;">print</span>(<span style="color: black;">s</span>)


    def body(<span style="color: black;">length</span>, *args):
    <span style="color: black;">s</span> = (<span style="color: black;">|%s|%s|%s|</span> %
    (args[<span style="color: black;">0</span>].center(<span style="color: black;">length</span>), str(args[<span style="color: black;">1</span>]).center(<span style="color: black;">5</span>), str(args[<span style="color: black;">2</span>]).center(<span style="color: black;">7</span>)))
    <span style="color: black;">print</span>(<span style="color: black;">s</span>)


    def output_student(lst):
    <span style="color: black;">length</span> = max(<span style="color: black;">map</span>(lambda <span style="color: black;">x</span>: len(<span style="color: black;">x</span>[<span style="color: black;">name</span>]), lst)) + <span style="color: black;">2</span>
    head(<span style="color: black;">length</span>) <span style="color: black;"># <span style="color: black;">起始</span>表格线+------+-----+-------+</span>body(<span style="color: black;">length</span>, <span style="color: black;">name</span>, <span style="color: black;">age</span>, <span style="color: black;">score</span>) <span style="color: black;"># 抬头行| name | age | score |</span>
    head(<span style="color: black;">length</span>) <span style="color: black;"># 分割表格线+------+-----+-------+</span>

    <span style="color: black;">for</span> i in lst:
    info = (i[<span style="color: black;">name</span>], i[<span style="color: black;">age</span>], i[<span style="color: black;">score</span>])
    body(<span style="color: black;">length</span>, *info) <span style="color: black;"># 内容</span>
    head(<span style="color: black;">length</span>) <span style="color: black;"># 结束表格线+------+-----+-------+</span>


    L = input_student()
    <span style="color: black;">print</span>(L)
    <span style="color: black;"># [{name: coco, age: 18, score: 100}, {name: gogo, age: 17, score: 60}]</span>
    output_student(L)<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/dfic-imagehandler/85ab903b-ef2a-43d6-a9e6-0ca6682d19a4~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723899749&amp;x-signature=MI12s2A8%2BsLPcJhFhSNIJVtQ6ec%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">41.改写学生信息管理项目源码,<span style="color: black;">需求</span>带有操作界面</h1><span style="color: black;">+--------------------------------+ # show_menu() <span style="color: black;">表示</span>功能<span style="color: black;">选取</span>菜单
      | 1) 添加学生信息 | # input_student()
      | 2) <span style="color: black;">表示</span>所有学生的信息 | # output_student() 可<span style="color: black;">做为</span>筛选的嵌套调用
      | 3) 删除学生信息 | # delete_student()
      | 4) 修改学生成绩 | # modify_student()
      | 5) 按学生成绩高-低<span style="color: black;">表示</span>学生信息 | # desc_score() 4种排序并一个函数sort_student()
      | 6) 按学生成绩低-高<span style="color: black;">表示</span>学生信息 | # asc_score()
      | 7) 按学生年龄高-低<span style="color: black;">表示</span>学生信息 | # desc_age()
      | 8) 按学生年龄低-高<span style="color: black;">表示</span>学生信息 | # asc_age()
      | 9) <span style="color: black;">保留</span>学生信息到文件(si.txt) | # write_file()
      | 10) 从文件中读取数据(si.txt) | # read_file()
      | q) 退出 | # quit()
      +--------------------------------+</span>
    <span style="color: black;"># <span style="color: black;">表示</span>功能<span style="color: black;">选取</span>菜单</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">show_menu</span><span style="color: black;">(lst)</span>:</span>
    <span style="color: black;"># <span style="color: black;">表示</span>菜单</span>
    show_list = [
    <span style="color: black;">+--------------------------------+</span>,
    <span style="color: black;">1) 添加学生信息</span>,
    <span style="color: black;">2) <span style="color: black;">表示</span>所有学生的信息</span>,
    <span style="color: black;">3) 删除学生信息</span>,
    <span style="color: black;">4) 修改学生成绩</span>,
    <span style="color: black;">5) 按学生成绩高-低<span style="color: black;">表示</span>学生信息</span>,
    <span style="color: black;">6) 按学生成绩低-高<span style="color: black;">表示</span>学生信息</span>,
    <span style="color: black;">7) 按学生年龄高-低<span style="color: black;">表示</span>学生信息</span>,
    <span style="color: black;">8) 按学生年龄低-高<span style="color: black;">表示</span>学生信息</span>,
    <span style="color: black;">9) <span style="color: black;">保留</span>学生信息到文件(si.txt)</span>,
    <span style="color: black;">10) 从文件中读取数据(si.txt)</span>,
    <span style="color: black;">q) 退出</span>,
    <span style="color: black;">+--------------------------------+</span>
    ]
    <span style="color: black;">for</span> row <span style="color: black;">in</span> show_list:
    print(row)

    <span style="color: black;"># <span style="color: black;">选取</span>菜单</span>
    menu_dic = {
    <span style="color: black;">1</span>: input_student, <span style="color: black;"># 添加</span>
    <span style="color: black;">2</span>: output_student, <span style="color: black;"># <span style="color: black;">表示</span></span>
    <span style="color: black;">3</span>: delete_student, <span style="color: black;"># 删除</span>
    <span style="color: black;">4</span>: modify_student,<span style="color: black;"># 修改</span>
    <span style="color: black;">5</span>: sort_student, <span style="color: black;"># 排序</span>
    <span style="color: black;">6</span>: sort_student,
    <span style="color: black;">7</span>: sort_student,
    <span style="color: black;">8</span>: sort_student,
    <span style="color: black;">9</span>: write_file, <span style="color: black;"># <span style="color: black;">保留</span>学生数据</span>
    <span style="color: black;">10</span>: read_file, <span style="color: black;"># 读取学生数据</span>
    <span style="color: black;">q</span>: quit <span style="color: black;"># 退出</span>
    }

    <span style="color: black;"># <span style="color: black;">选取</span>功能</span>choice = input(<span style="color: black;">&gt;&gt;&gt;</span>) <span style="color: black;">or</span> <span style="color: black;">-1</span>
    <span style="color: black;">if</span> choice <span style="color: black;">in</span> menu_dic:
    <span style="color: black;">if</span> choice == <span style="color: black;">6</span>:
    menu_dic(lst, desc=<span style="color: black;">False</span>)
    <span style="color: black;">elif</span> choice == <span style="color: black;">7</span>:
    menu_dic(lst, basis=<span style="color: black;">age</span>)
    <span style="color: black;">elif</span>choice ==<span style="color: black;">8</span>:
    menu_dic(lst, basis=<span style="color: black;">age</span>, desc=<span style="color: black;">False</span>)
    <span style="color: black;">else</span>:
    menu_dic(lst)
    <span style="color: black;">else</span>:
    print(<span style="color: black;">"输入有误,请从新<span style="color: black;">选取</span>"</span>)


    <span style="color: black;"># 1.添加学生信息</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">input_student</span><span style="color: black;">(lst)</span>:</span>
    <span style="color: black;">while</span> <span style="color: black;">True</span>:
    name = input(<span style="color: black;">"请输入学生姓名:"</span>) <span style="color: black;">or</span> <span style="color: black;">-1</span>
    <span style="color: black;">if</span> name == <span style="color: black;">-1</span>: <span style="color: black;">break</span>
    age = input(<span style="color: black;">"请输入学生年龄:"</span>)
    score = input(<span style="color: black;">"请输入学生成绩:"</span>)
    lst.append({<span style="color: black;">name</span>: name,
    <span style="color: black;">age</span>: age,
    <span style="color: black;">score</span>: score
    })


    <span style="color: black;"># 2.<span style="color: black;">表示</span>所有学生信息</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">output_student</span><span style="color: black;">(lst)</span>:</span>

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">head</span><span style="color: black;">(length)</span>:</span>
    s = <span style="color: black;">+%s+%s+%s+</span> % (<span style="color: black;">-</span> * (length), <span style="color: black;">-</span> * <span style="color: black;">5</span>, <span style="color: black;">-</span> * <span style="color: black;">7</span>)
    print(s)

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">body</span><span style="color: black;">(length, *args)</span>:</span>
    s = <span style="color: black;">|%s|%s|%s|</span> % (args[<span style="color: black;">0</span>].center(length), args[<span style="color: black;">1</span>].center(<span style="color: black;">5</span>), args[<span style="color: black;">2</span>].center(<span style="color: black;">7</span>))
    print(s)

    <span style="color: black;">if</span> lst:
    length = max(map(<span style="color: black;">lambda</span> x: len(x[<span style="color: black;">name</span>]), lst)) + <span style="color: black;">2</span>head(length)<span style="color: black;"># <span style="color: black;">起始</span>表格线+------+-----+-------+</span>
    body(length, <span style="color: black;">name</span>, <span style="color: black;">age</span>, <span style="color: black;">score</span>) <span style="color: black;"># 抬头行| name | age | score |</span>
    head(length) <span style="color: black;"># 分割表格线+------+-----+-------+</span>

    <span style="color: black;">for</span> i <span style="color: black;">in</span> lst:
    info = (i[<span style="color: black;">name</span>], i[<span style="color: black;">age</span>], i[<span style="color: black;">score</span>])
    body(length, *info) <span style="color: black;"># 内容</span>
    head(length) <span style="color: black;"># 结束表格线+------+-----+-------+</span>
    <span style="color: black;">else</span>:
    print(<span style="color: black;">"<span style="color: black;">无</span>学生信息,请<span style="color: black;">选取</span>: 1)添加学生信息"</span>)


    <span style="color: black;"># 3.删除学生信息</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">delete_student</span><span style="color: black;">(lst)</span>:</span>
    choice = input(<span style="color: black;">"请输入要删除学生的姓名:"</span>) <span style="color: black;">or</span> <span style="color: black;">-1</span>
    sign = <span style="color: black;">False</span>

    <span style="color: black;">for</span> i, item <span style="color: black;">in</span> enumerate(lst):
    <span style="color: black;">if</span> choice == item[<span style="color: black;">name</span>]:
    sign = <span style="color: black;">True</span>
    <span style="color: black;">break</span>
    <span style="color: black;">else</span>:
    print(<span style="color: black;">"你要删除的学生不存在"</span>)

    <span style="color: black;">if</span> sign:
    lst.pop(i)
    print(<span style="color: black;">"已删除学生:%s的信息"</span> % choice)


    <span style="color: black;"># 修改学生成绩</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">modify_student</span><span style="color: black;">(lst)</span>:</span>
    choice = input(<span style="color: black;">"请输入要修改学生的姓名:"</span>) <span style="color: black;">or</span> <span style="color: black;">-1</span>

    <span style="color: black;">for</span> item <span style="color: black;">in</span> lst:
    <span style="color: black;">if</span>choice == item[<span style="color: black;">name</span>]:
    temp_list =
    output_student(temp_list)
    score = input(<span style="color: black;">"请输入新的成绩:"</span>) <span style="color: black;">or</span> <span style="color: black;">-1</span>
    item[<span style="color: black;">score</span>] = score
    print(<span style="color: black;">"已修改学生:%s的成绩"</span>% choice)<span style="color: black;">break</span>
    <span style="color: black;">else</span>:
    print(<span style="color: black;">"你要修改成绩的学生不存在"</span>)


    <span style="color: black;"># 排序--&gt;按<span style="color: black;">选取</span>的<span style="color: black;">要求</span>排序列表后,<span style="color: black;">做为</span>实参传递给<span style="color: black;">表示</span>函数output_student(lst)</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">sort_student</span><span style="color: black;">(lst, basis=<span style="color: black;">score</span>, desc=True)</span>:</span> <span style="color: black;"># 默认按年龄大到小</span>lst = sorted(lst, key=<span style="color: black;">lambda</span> dic: int(dic), reverse=desc)

    output_student(lst)


    <span style="color: black;"># <span style="color: black;">保留</span>学生信息</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">write_file</span><span style="color: black;">(lst, file=<span style="color: black;">"si.txt"</span>)</span>:</span>
    <span style="color: black;">try</span>:
    f = open(file, <span style="color: black;">"w"</span>)<span style="color: black;">for</span> i <span style="color: black;">in</span> lst:
    s = <span style="color: black;">"%s,%s,%s\n"</span> % (i[<span style="color: black;">name</span>], i[<span style="color: black;">age</span>], i[<span style="color: black;">score</span>])
    f.write(s)
    f.flush()
    f.close()
    print(<span style="color: black;">"已成功<span style="color: black;">保留</span>学生信息到[%s]文件"</span> % file)
    <span style="color: black;">except</span>IOError:
    print(<span style="color: black;">"文件打开失败"</span>)


    <span style="color: black;"># 读取学生信息</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">read_file</span><span style="color: black;">(lst, file=<span style="color: black;">"si.txt"</span>)</span>:</span>
    <span style="color: black;">try</span>:
    f = open(file)
    <span style="color: black;">while</span> <span style="color: black;">True</span>:
    s = f.readline()
    <span style="color: black;">if</span> <span style="color: black;">not</span> s:
    f.close()
    <span style="color: black;">break</span>s = s.rstrip()
    s = s.split(<span style="color: black;">","</span>)
    dic = {<span style="color: black;">name</span>: s[<span style="color: black;">0</span>], <span style="color: black;">age</span>: s[<span style="color: black;">1</span>], <span style="color: black;">score</span>: s[<span style="color: black;">2</span>]}
    lst.append(dic)
    print(<span style="color: black;">"已成功从文件:[%s]读取到学生信息"</span> % file)
    <span style="color: black;">except</span>IOError:
    print(<span style="color: black;">"文件打开失败"</span>)


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">main</span><span style="color: black;">()</span>:</span>
    <span style="color: black;">"""学生信息管理项目主<span style="color: black;">规律</span>入口"""</span>
    <span style="color: black;">while</span> <span style="color: black;">True</span>:
    show_menu(L)


    <span style="color: black;">if</span> __name__ == <span style="color: black;">"__main__"</span>:
    L = []
    main()




流星的美 发表于 2024-8-27 04:42:59

你的话深深触动了我,仿佛说出了我心里的声音。

4zhvml8 发表于 2024-10-17 17:28:28

“板凳”(第三个回帖的人)‌
页: [1]
查看完整版本: Python语法示例——函数