本文主要是介绍python编写斐波那契数螺旋线+正方形(彩色)(讲解见分布代码)(适合学过编程6个月~1年11个月的人(中低级)),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一.这是什么
简单来说:第1,2两项是1,从第3项开始,每一项都是前两项的和(前10项:1,1,2,3,5,8,13,21,34,55)
二.初始设置
from turtle import*#导入相关内容
from random import randint
speed(0)#设为最快
list=["2","3","4","5","6","7","8","9","10","11","12","13"]#设置允许数量
colormode(255)#RGB可从000~255之间设置
三.定义函数
def cir(r):#画1/4圆fillcolor(((randint(0,255)),(randint(0,255)),(randint(0,255))))#上色begin_fill()circle(0-r,extent=90)#由于默认是逆时针的,所以要反过来for _ in range(2):#复位rt(90)fd(r)rt(90)end_fill()
def squ(d):#画方形fillcolor((randint(0,255),randint(0,255),randint(0,255)))#上色begin_fill()for __ in range(4):fd(d)rt(90)end_fill()
def draw(rd):#结合前两个函数squ(rd)#大的先cir(rd)circle(0-rd,extent=90)
四.用户设置
number=input("输入个数(02~13)")#设置数量
五.检测绘画
if number not in list:#检查是否在列表里print("输入错误")
else:number=int(number)#化成整数a=1draw(1)#调用函数print(1)#输出b=1#设置另一个数draw(1)print(1,end="\n")for step in range(number-2):#已经出现2个1,要扣掉c=a+bdraw(c)print(a,"+",b,"=",c,sep="",end="\n")a=bb=c
六.完整代码
from turtle import*
from random import randint
speed(0)
list=["2","3","4","5","6","7","8","9","10","11","12","13"]
colormode(255)
def cir(r):fillcolor(((randint(0,255)),(randint(0,255)),(randint(0,255))))begin_fill()circle(0-r,extent=90)for _ in range(2):rt(90)fd(r)rt(90)end_fill()
def squ(d):fillcolor((randint(0,255),randint(0,255),randint(0,255)))begin_fill()for __ in range(4):fd(d)rt(90)end_fill()
def draw(rd):squ(rd)cir(rd)circle(0-rd,extent=90)
number=input("输入个数(02~13)")
if number not in list:print("输入错误")
else:number=int(number)a=1draw(1)print(1)b=1draw(1)print(1,end="\n")for step in range(number-2):c=a+bdraw(c)print(a,"+",b,"=",c,sep="",end="\n")a=bb=c
七.运行结果(输入13)
7-1.图案结果
7-2.数的结果
1
1
1+1=2
1+2=3
2+3=5
3+5=8
5+8=13
8+13=21
13+21=34
21+34=55
34+55=89
55+89=144
89+144=233
这篇关于python编写斐波那契数螺旋线+正方形(彩色)(讲解见分布代码)(适合学过编程6个月~1年11个月的人(中低级))的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!