继续做题~经过py3测试

原题链接:http://www.runoob.com/python/python-exercise-example6.html

题目:斐波那契数列。

我的代码:

def fib(n):
if n==1:        
l=[0]
elif n==2:
l=[0,1]
else:
l=[0,1]
for i in range(2,n):
l.append(l[i-1]+l[i-2])
return l

思考:函数fib是以列表的形式返回的斐波那契数列,该列表第0/1位的无法迭代得出,所以用了if来解决这个问题。但是判断的次数很多,其实代码很繁琐。

看了题目下面的答案,可以用迭代的形式一次输出一个数字,代码也简洁了很多~

def Fib(n):
a, b = 0, 1       #直接用变量给出了1,2个数,不需要if         
print(a)              
while n-1:
a, b, n = b, a + b, n - 1 #用一行代码就实现了斐波那契数列的迭代,也实现了循环的计数
print(a)

python3练习100题——006的更多相关文章

  1. python3练习100题——003

    今天继续-答案都会通过python3测试- 原题链接:http://www.runoob.com/python/python-exercise-example3.html 题目:一个整数,它加上100 ...

  2. python3练习100题——002

    因为特殊原因,昨天没有做题.今天继续- 原题链接:http://www.runoob.com/python/python-exercise-example2.html 题目: 企业发放的奖金根据利润提 ...

  3. python3练习100题——004

    继续做题-经过python3的测试 原题链接:http://www.runoob.com/python/python-exercise-example4.html 题目:输入某年某月某日,判断这一天是 ...

  4. python3练习100题——036

    原题链接:http://www.runoob.com/python/python-exercise-example36.html 题目:求100之内的素数. 之前有类似的题,所以这次遇到觉得很容易了, ...

  5. python3练习100题——035

    原题链接:http://www.runoob.com/python/python-exercise-example34.html 题目:文本颜色设置. 学习了一下python3 的文本颜色设置. 其实 ...

  6. python3练习100题——020

    原题链接:http://www.runoob.com/python/python-exercise-example20.html 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半:再落下 ...

  7. python3练习100题——013

    熟悉的水仙花数来了,,,... 原题链接:http://www.runoob.com/python/python-exercise-example13.html 题目:打印出所有的"水仙花数 ...

  8. python3练习100题——056

    题目:画图,学用circle画圆形. 可以用turtle.circle画图. import turtle turtle.setup(0.6,0.6) turtle.pensize(3) turtle. ...

  9. python3练习100题——050

    题目:输出一个随机数. 程序分析:使用 random 模块. import random print( random.randint(1,10) ) # 产生 1 到 10 的一个整数型随机数 pri ...

随机推荐

  1. opencv —— createTrackbar、getTrackbarPos 滑动条的创建和使用

    创建滑动条:createTrackbar 函数 createTrackbar 函数用于创建一个可以调整数值的滑动条,并将滑动条附加在指定的窗口上. int createTrackbar(const s ...

  2. ajax-属性、原理、实现html5进度条上传文件

    一.远古ajax实现方式如下: 1.前端请求后台,后台设置返回 http状态码204 2.运用img图片(或css/javascript等)的加载机制,请求后台 3.post提交数据,运用iframe ...

  3. 【终端使用】"scp"命令,远程拷贝文件

    一."scp"命令的使用 "scp"命令,是"secure copy (remote file copy program)"英文单词的缩写, ...

  4. CVE-2019-1388 UAC提权复现

    0x01 前言 该漏洞位于Windows的UAC(User Account Control,用户帐户控制)机制中.默认情况下,Windows会在一个单独的桌面上显示所有的UAC提示--Secure D ...

  5. oracle DB 使用注意点小结

    1.DDL 后不需要添加commit;语句,因为Oracle数据库的DDL不支持transaction,执行即commit: DDL(Data Definition Language): 数据定义语言 ...

  6. react 中 函数bind 和箭头函数

    用bind形式 方便测试,含有this时候最好用bind形 其他情况用箭头函数 含有this的时候也可以用箭头函数

  7. 注解配置springMVC

    在随笔“springMVC项目配置文件”的基础上,进行优化,使用注解配置,控制器类得以简化: 一.注解配置springMVC 1.在HelloController类中,去除实现的Controller接 ...

  8. eclipse配置文件出现小红叉,Referenced file contains errors (xml文件第一行小红叉错误)

    原文链接:https://blog.csdn.net/zlj1217/article/details/61432437                                          ...

  9. rtp传输h264

    ---恢复内容开始--- 基本概念的理解 H.264的主要目标:1.高的视频压缩比2.良好的网络亲和性 解决方案:VCL video coding layer 视频编码层NAL network abs ...

  10. shell-删除指定时间前的文件

    需要配合find和rm两个命令完成 显示20分钟前的文件: find /home/prestat/bills/test -type f -mmin +20 -exec ls -l {} \; 删除20 ...