中国大学MOOC課程「Python言語プログラミング」授業後練習(2週目)
7405 ワード
中国大学MOOC課程「Python言語プログラミング」授業後練習(2週目)
1、蛇プログラムの描画:
2、eval関数機能:文字列strを有効な式として評価し、計算結果を返す.
構文:eval(source[,globals[,locals]])->value
パラメータ:
source:Python式または関数compile()が返すコードオブジェクト
globals:オプション.ディクショナリでなければなりません
locals:オプション.任意mapオブジェクト
3、カラーボアを描く
異なる色を固定:
import turtle import random pencolors=[‘red’,’blue’,’yellow’,’black’] def drawSnake(rad,angle,len,neckrad): turtle.pencolor(pencolors[3]) for i in range(len): turtle.circle(rad,angle) turtle.circle(-rad,angle) turtle.pencolor(pencolors[1]) turtle.circle(rad,angle/2) turtle.pencolor(pencolors[0]) turtle.fd(rad) turtle.circle(neckrad+1,180) turtle.fd(rad*2/3)
def main(): turtle.setup(1300,800,0,0) pythonsize=30 turtle.pensize(pythonsize) turtle.seth(-40) drawSnake(40,80,5,pythonsize/2)
main()
import turtle
for i in range(3): turtle.fd(50) turtle.left(120)
“`
1、蛇プログラムの描画:
import turtle
def drawSnake(rad,angle,len,neckrad):
for i in range(len):
turtle.circle(rad,angle) # ,
turtle.circle(-rad,angle)
turtle.circle(rad,angle/2)
turtle.fd(rad)
turtle.circle(neckrad+1,180) # 180
turtle.fd(rad*2/3)
def main():
turtle.setup(1300,800,0,0) #
pythonsize=3
turtle.pensize(pythonsize)
turtle.pencolor("blue")
turtle.seth(-40) #
drawSnake(40,80,5,pythonsize/2)
main()
2、eval関数機能:文字列strを有効な式として評価し、計算結果を返す.
構文:eval(source[,globals[,locals]])->value
パラメータ:
source:Python式または関数compile()が返すコードオブジェクト
globals:オプション.ディクショナリでなければなりません
locals:オプション.任意mapオブジェクト
list,tuple,dict string 。
#################################################
>>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
>>>type(a)
'str'>
>>> b = eval(a)
>>> print b
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
>>> type(b)
'list'>
#################################################
>>> a = "{1: 'a', 2: 'b'}"
>>> type(a)
'str'>
>>> b = eval(a)
>>> print b
{1: 'a', 2: 'b'}
>>> type(b)
'dict'>
#################################################
>>> a = "([1,2], [3,4], [5,6], [7,8], (9,0))"
>>> type(a)
'str'>
>>> b = eval(a)
>>> print b
([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
>>> type(b)
'tuple'>
3、カラーボアを描く
import turtle
import random
pencolors=['red','blue','yellow','black']
def drawSnake(rad,angle,len,neckrad):
for i in range(len):
turtle.circle(rad,angle)
turtle.circle(-rad,angle)
pencolor()
turtle.circle(rad,angle/2)
pencolor()
turtle.fd(rad)
turtle.circle(neckrad+1,180)
pencolor()
turtle.fd(rad*2/3)
def main():
turtle.setup(1300,800,0,0)
pythonsize=3
turtle.pensize(pythonsize)
pencolor()
turtle.seth(-40)
drawSnake(40,80,5,pythonsize/2)
def pencolor():
turtle.pencolor(pencolors[random.randint(0,3)])
main()
異なる色を固定:
import turtle import random pencolors=[‘red’,’blue’,’yellow’,’black’] def drawSnake(rad,angle,len,neckrad): turtle.pencolor(pencolors[3]) for i in range(len): turtle.circle(rad,angle) turtle.circle(-rad,angle) turtle.pencolor(pencolors[1]) turtle.circle(rad,angle/2) turtle.pencolor(pencolors[0]) turtle.fd(rad) turtle.circle(neckrad+1,180) turtle.fd(rad*2/3)
def main(): turtle.setup(1300,800,0,0) pythonsize=30 turtle.pensize(pythonsize) turtle.seth(-40) drawSnake(40,80,5,pythonsize/2)
main()
4、
5、
import turtle
for i in range(3): turtle.fd(50) turtle.left(120)
“`