A. 用python怎麼編寫這兩道題
i=1
while i <= 7:
i=1
while i<=j :
print(" ", i, end = "")
i = i+1
print("")
j=j+1
輸出結果:
B. python可以畫出哪些簡單圖形
一、畫一朵花+簽名
代碼如下:
# -*- coding:utf-8 -*-
#畫一朵花+簽名
import turtle
turtle.color('red','green')
turtle.pensize(5)
turtle.goto(0,0)
turtle.speed(10)
for i in range(15):
turtle.forward(100)
turtle.right(150)
turtle.up()
turtle.goto(150,-120)
turtle.color('black')
turtle.write("xxx" )
turtle.up()
turtle.goto(160,-140)
turtle.color('black')
turtle.write("2018 年 1 月 10 日" )
turtle.up()
turtle.goto(240,-160)
turtle.color('black')
turtle.write("." )
turtle.done()
二、畫五角星臉+簽名
代碼如下:
# -*- coding:utf-8 -*-
#畫五角星臉+簽名
import turtle
turtle.color('red','green')
turtle.pensize(5)
turtle.forward(100)
turtle.right(144)
turtle.forward(100)
turtle.right(144)
turtle.forward(100)
turtle.right(144)
turtle.forward(100)
turtle.right(144)
turtle.forward(100)
turtle.right(144)
turtle.forward(100)
turtle.up()
turtle.goto(150,120)
turtle.down()
turtle.color('red','green')
turtle.forward(50)
turtle.right(144)
turtle.forward(50)
turtle.right(144)
turtle.forward(50)
turtle.right(144)
turtle.forward(50)
turtle.right(144)
turtle.forward(50)
turtle.up()
turtle.goto(-80,90)
turtle.down()
turtle.color('red','green')
turtle.forward(50)
turtle.right(144)
turtle.forward(50)
turtle.right(144)
turtle.forward(50)
turtle.right(144)
turtle.forward(50)
turtle.right(144)
turtle.forward(50)
turtle.up()
turtle.goto(150,-120)
turtle.color('black')
turtle.write("xxx" )
turtle.up()
turtle.goto(160,-140)
turtle.color('black')
turtle.write("2018 年 1 月 7 日" )
turtle.up()
turtle.goto(240,-160)
turtle.color('black')
turtle.write("." )
turtle.done()
C. python 如何繪制下面這種圖形
matplotlib可以畫,這是幫助鏈接
matplotlib Stem Plot
效果如下
D. Python 八宮格,橫豎斜相鄰數字不連續,1-8填入,要求輸出為八宮格樣式,求高手核心代碼
importitertools
defcheck(cells):
forrinrange(1,3):
forcinrange(2):
ifabs(cells[r][c]-cells[r][c+1])==1:
returnFalse
forcinrange(3):
forrinrange(3):
ifabs(cells[r][c]-cells[r+1][c])==1:
returnFalse
forrinrange(3):
forcinrange(3):
ifc!=0:
ifabs(cells[r][c]-cells[r+1][c-1])==1:
returnFalse
ifc!=2:
ifabs(cells[r][c]-cells[r+1][c+1])==1:
returnFalse
returnTrue
cells=[[-1foriinrange(3)]forjinrange(4)]
forpinitertools.permutations(range(1,9),8):
cells[0][1]=p[0]
cells[1][0]=p[1]
cells[1][1]=p[2]
cells[1][2]=p[3]
cells[2][0]=p[4]
cells[2][1]=p[5]
cells[2][2]=p[6]
cells[3][1]=p[7]
ifcheck(cells):
print(cells)
暴力O(8!)
E. 如何用Python畫各種著名數學圖案
如何用Python畫各種著名數學圖案 | 附圖+代碼
用Python繪制著名的數學圖片或動畫,展示數學中的演算法魅力。
Mandelbrot 集
'''
A fast Mandelbrot set wallpaper renderer
reddit discussion:
'''
importnumpy asnp
fromPILimportImage
fromnumba importjit
MAXITERS=200
RADIUS=100
@jit
defcolor(z, i):
v =np.log2(i +1-np.log2(np.log2(abs(z)))) /5
ifv <1.0:
returnv**4, v**2.5, v
else:
v =max(0, 2-v)
returnv, v**1.5, v**3
@jit
defiterate(c):
z =0j
fori inrange(MAXITERS):
ifz.real*z.real +z.imag*z.imag >RADIUS:
returncolor(z, i)
z =z*z +c
return0, 0,0
defmain(xmin, xmax, ymin, ymax, width, height):
x =np.linspace(xmin, xmax, width)
y =np.linspace(ymax, ymin, height)
z =x[None, :] +y[:, None]*1j
red, green, blue =np.asarray(np.frompyfunc(iterate, 1, 3)(z)).astype(np.float)
img =np.dstack((red, green, blue))
Image.fromarray(np.uint8(img*255)).save('mandelbrot.png')
if__name__=='__main__':
main(-2.1, 0.8, -1.16, 1.16, 1200, 960)
F. python如何給八邊形填色
-設定填充色:fillecolor(r, g, b)
-開始填充:begin_fill()
-結束填充:end_fill()
畫一組隨機分布,隨機大小和不同色調的心形。先初始化一個填充顏色。然後,在畫每個圖形繪制之前使用begin_fill()以及繪制之後使用end_fill()。這樣就能得到一個填充效果。
import turtle as t
import random as r
def pink():
color = (1, r.random(), 1)
return color
def randomrange(min, max):
return min + (max- min)*r.random()
def moveto(x, y):
t.penup()
t.goto(x, y)
t.pendown()
def heart(r, a):
factor = 180
t.seth(a)
t.circle(-r, factor)
t.fd(2 * r)
t.right(90)
t.fd(2 * r)
t.circle(-r, factor)
t.setup(800, 800, 200, 200)
t.speed(9)
t.pensize(1)
t.penup()
for i in range(20):
t.goto(randomrange(-300, 300), randomrange(-300, 300))
t.begin_fill()
t.fillcolor(pink())
heart(randomrange(10, 50), randomrange(0, 90))
t.end_fill()
moveto(400, -400)
t.done()
G. 怎麼用python的turtle庫畫出這個圖案,要代碼
import turtle as t
def quad(color):
t.begin_fill()
t.color(color)
t.forward(100)
t.left(36)
t.forward(100)
t.left(36*4)
t.forward(100)
t.left(36)
t.forward(100)
t.end_fill()
t.left(36*3)
for i in range(10):
if i%2:
quad('#99c8de')
else:
quad('#e5b9c4')
兩三年沒碰海龜了,覺得沒啥用,看你賞金又提了就回去學了學
H. python 語言怎麼畫八卦圖
fromturtleimport*
defyin(radius,color1,color2):
width(3)
color("black",color1)
begin_fill()
circle(radius/2.,180)
circle(radius,180)
left(180)
circle(-radius/2.,180)
end_fill()
left(90)
up()
forward(radius*0.35)
right(90)
down()
color(color1,color2)
begin_fill()
circle(radius*0.15)
end_fill()
left(90)
up()
backward(radius*0.35)
down()
left(90)
defmain():
reset()
yin(200,"black","white")
yin(200,"white","black")
ht()
return"Done!"
if__name__=='__main__':
main()
mainloop()
#這是太極圖,自行添加八卦符號吧
I. python turtle 八角形的製作
fromturtleimport*
fillcolor('pink')
begin_fill()
foriinrange(8):
forward(100)
right(45)
end_fill()
exitonclick()
假設每一條邊長長為100
畫八邊形需要在前進100後向右轉45度,重復(也就是循環)8次即可
J. Python如何運用turtle繪制陰陽太極圖
本文詳細分析如何使用Python turtle繪制陰陽太極圖,先來分解這個圖形,圖片中有四種顏色,每條曲線上的箭頭表示烏龜移動的方向,首先從中心畫一個半圓(紅線),以紅線所示圓的直徑作半徑畫一個校園,半徑為紅線所示圓半徑的0.15倍(藍線),之所以選擇0.15倍,是因為這樣嵌入紅圓內的小圓直徑和紅圓直徑接近黃金分割。
相關推薦:《Python視頻教程》
完整代碼:
效果圖如下: