[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>函数的设计
<>实验目标
1. 熟练掌握函数的设计和使用
2. 深入理解各类函数,熟悉参数传递过程
3. 掌握变量的作用域、生成器函数和lambda函数
4. 了解函数的递归和使用方法。
<>实验过程
注:本实验使用Jupyter Notebook编译,并已使用pycharm进行可行性验证。
<>
4.编写函数,判断一个数是否为水仙花数。调用该函数打印出1000以内的所有水仙花数。水仙花数是指一个n位数(n≥3),它的每个位上的数字的n次幂之和等于它本身。
例如: 1 3 1^3 13+ 5 3 5^3 53+ 1 3 1^3 13
=153,则153是水仙花数。水仙花数只是自幂数的一种,严格来说三位数的3次幂数才成为水仙花数。
参考代码:
def f(n): if 100 < n < 1000 : bw = n // 100 sw = (n - bw * 100) // 10 gw = n %
10 if n == bw ** 3 + sw ** 3 + gw ** 3: print(n,end=' ') for i in range(1,1000):
f(i)
实验截图:
<>
5.编写函数求斐波那契数列的前20项。斐波拉契数列的第1项和第2项分别是0和1,从第3项开始,每一项都是前两项之和。如:0,1,1,2,3,5,8,13,21…。试用递归函数实现。
参考代码:
def fibo(n): if n <= 1: return n else: return (fibo(n - 1) + fibo(n - 2)) n =
int(input("斐波那契数列的项数 ")) if n <= 0: print("输入正数") else: print("斐波那契数列:") for i
in range(n): print(fibo(i))
实验截图:
如有错误或者更好的方法,敬请批评指正!