روز شنبه پس از توضیحات کافی در کلاس کوییزی با موضوع برجهای هانوی برگزار خواهد شد.
http://en.wikipedia.org/wiki/Tower_of_Hanoi
from turtle import * speed(0) x = 50 y = 70 dx = 5 dy = 5 minx = 0 miny = 0 maxx, maxy = 200 , 100 def update(x, y, dx, dy, minx , miny, maxx, maxy): if y < miny or y > maxy: dy = -1 * dy if x < minx or x > maxx: dx = -1 * dx x = x + dx y = y + dy return x, y, dx, dy def draw(x, y, dx, dy, minx , miny, maxx, maxy): begin_fill() goto(x,y) clear() circle(10) end_fill() for t in range(200): x, y, dx, dy = update(x, y, dx, dy, minx , miny, maxx, maxy) draw(x, y, dx, dy, minx , miny, maxx, maxy)تمرین 1:
from turtle import * speed(0) hideturtle() x = 50 y = 70 z = 60 dx = 5 dy = 5 dz = 5 minx = 0 miny = 0 maxx, maxy = 200 , 100 minz, maxz = 0 , 200 def update(x, y, z, dx, dy, dz, minx , miny, minz, maxx, maxy, maxz): if y < miny or y > maxy: dy = -1 * dy if x < minx or x > maxx: dx = -1 * dx if z < minz or z > maxz: dz = -1 * dz x = x + dx y = y + dy z = z + dz return x, y, z, dx, dy, dz def draw(x, y, z, dx, dy, dz, minx , miny, minz, maxx, maxy, maxz): begin_fill() goto(x,y) clear() circle(z/20) end_fill() for t in range(200): x, y, z, dx, dy, dz = update(x, y, z, dx, dy, dz, minx , miny, minz, maxx, maxy, maxz) draw(x, y, z, dx, dy, dz, minx , miny, minz, maxx, maxy, maxz)
def koch(a,b,c,d,n): if n>0: ht() x=abs(a-c) y=abs(b-d) l=sqrt(x**2+y**2) pencolor("#000000") pu() goto(a,b) th=towards(c,d) pd() seth(th) fd(l/3.0) p=pos()[0] q=pos()[1] seth(th+60) fd(l/3.0) u=pos()[0] v=pos()[1] seth(th-60) fd(l/3.0) j=pos()[0] k=pos()[1] pd() seth(th) pencolor("#FFFFFF") bk(l/3.0) fd(l/3.0) pencolor("#000000") fd(l/3.0) koch(p,q,u,v,n-1) koch(u,v,j,k,n-1) koch(a,b,p,q,n-1) koch(j,k,c,d,n-1) from math import * import os from turtle import * speed(0) N=4 # Depth of Fractal koch(-200,80,200,80,N) koch(0,80-200*sqrt(3.0),-200,80,N) koch(200,80,0,80-200*sqrt(3.0),N) pu() goto(-80,-50) write("Koch Snowflake",False,"left",("Tahoma", 18, "italic")) os.system("pause")
>>> for x in range(9): ... print x, ... 0 1 2 3 4 5 6 7 8 >>> for c in azin: ... print c ... Traceback (most recent call last): File "دستور range", line 1, in NameError: name 'azin' is not defined >>> for c in "azin": ... print c ... a z i n
>>> range(5) [0, 1, 2, 3, 4] >>> range(0,9) [0, 1, 2, 3, 4, 5, 6, 7, 8] >>> range(0,9,3) [0, 3, 6]
>>> for x in ["egg", "apple", "orange"]: ... print x, ... egg apple orange
>>> a = ["egg", "apple", "orange"] >>> for x in a: ... print x, ... egg apple orange >>> a[1] 'apple' >>> a[0] 'egg' >>> a[-1] 'orange' >>> a.append(34) >>> a ['egg', 'apple', 'orange', 34]
>>> a = [1, 3, 5, 6] >>> sum = 0 >>> for x in a: ... sum = sum + x ... >>> print sum 15
from turtle import * for x in range(-32, 33, 8): penup() goto(-32, x) pendown() goto(32,x) penup() goto(x,-32) pendown() goto(x,32)
محمد حسین:
from turtle import * for x in range(20,161,20): fd(x) rt(90) fd(x) rt(90) fd(x) rt(90) fd(x) rt(90) pu() goto(160,-160) rt(180) pendown() for x in range(20,141,20): fd(x) rt(90) fd(x) rt(90) fd(x) rt(90) fd(x) rt(90)
from turtle import * c=["yellow","","black"] a=1 speed(500) for y in range (0,141,20): a=a*-1 for x in range (0,141,20): pu() goto(x,y) pd() a=a*-1 fillcolor(c[a+1]) begin_fill() seth(0) fd(20) left(90) fd(20) left(90) fd(20) end_fill() pu() goto(0,0) pd() seth(0) for i in range(4): fd(160) left(90) pu()
goto(140,-50) write("by Hirbod")برنامه نوشته شده توسط نگین:
from turtle import * goto (0,0) goto (80,0) goto (80,80) goto (0,80) goto (0,0) x=y=-20 for a in range (4): x=x+20 y=-20 for b in range (4): y=y+20 pu () goto (x,y) begin_fill () goto (x+10,y) goto (x+10,y+20) goto (x+20, y+20) goto (x+20,y+10) goto (x,y+10) end_fill ()میلاد:
from turtle import * s=Shape("compound") c="black" s.addcomponent(((0,0),(0,160),(40,160),(40,0)),"",c) for y in range(0,141,40): s.addcomponent(((0,y+20),(0,y),(20,y),(20,y+40),(40,y+40),(40,y+20)),c,c) register_shape("p", s) shape("p") stamp() sety(-40) stamp() sety(-80) stamp() sety(-120)
برچسبها: lessons
a = 2 b = 4 print a + b c = "hi," d = " bye!" print c + d
print 2 + 3 * 4 print 2 ** 3
from turtle import * forward(100) right(90) forward(100) right(90) forward(100) right(90)
for x in range(1,5): forward(100) right(90)
for r in range(10,51,10): circle(r)
آرگومان سوم دستور range (در اینجا 10) طول قدم را مشخص میکند، قطعه کد بالا در حقیقت معادل با اجرای قطعه کد زیر است:
r=10 circle(r) r=20 circle(r) r=30 circle(r) r=40 circle(r) r=50 circle(r)
برچسبها: lessons
public class A{ public static void main(String args[]){ System.out.println("Hello world"); } }
print "hello"
برچسبها: lessons