Silver Announcements


Lesson 17: Bar charts with Turtle and exponential growth

posted Mar 12, 2020, 8:04 AM by Attila Lengyel

import random as r

#series = [0,0,0,0,0,0]
#for x in range(10000):
# dice = r.randint(1,6)
# series[dice-1] = series[dice-1] + 1

series = [1]
for x in range(14):
growth= series[len(series)-1]
growth= growth* 2;
series.append(growth)

import turtle as t
st = t.Turtle()

st.penup()
st.setposition(-200,-200)
st.pendown()
st.fd(400)
st.penup()
st.setposition(-200,-200)
st.pendown()
st.left(90)
st.fd(400)

x = -230
for s in series:
x = x + 40
st.penup()
st.setposition(x,-200)
st.pendown()
height = (400 * s) / max(series)
st.begin_fill()
st.fd(height)
st.write(s)
st.right(90)
st.fd(20)
st.right(90)
st.fd(height)
st.end_fill()
st.right(180)



Lesson 16: Squares with Turtle

posted Mar 5, 2020, 8:02 AM by Attila Lengyel

import turtle as t
bob = t.Turtle()

bob.speed(30)
x = [-160,-130,-100, -70, -40, -10]
y = [160, 130, 100, 70, 40, 10]

for a in x:
for b in y:
bob.penup()
bob.setposition(a,b)
bob.pendown()

for i in range(4):
bob.fd(30)
bob.right(90)

Lesson 14: Two turtle chasing each other in a box

posted Feb 13, 2020, 8:04 AM by Attila Lengyel

import turtle as t

john = t.Turtle()
john.shape("turtle")
john.fillcolor("blue")
john.pencolor("blue")

bob = t.Turtle()
bob.shape("turtle")
bob.fillcolor("red")
bob.pencolor("red")
bob.penup()

screen = t.Screen()

john.penup()
john.setposition(-200,200)
john.pendown()
for x in range(4):
  john.fd(400)
  john.right(90)
john.penup()
john.setposition(-190,190)


def move_forward():
  john.pencolor("blue")
  john.forward(9)
  if john.xcor() < -200 and john.ycor()> 200:
    john.setposition(-199,199)
  if  john.xcor() > 200 and john.ycor()> 200: 
    john.setposition(199,199)
  if john.xcor() > 200 and john.ycor() < -200: 
    john.setposition(199,-199)
  if john.xcor() < -200 and john.ycor()<-200: 
    john.setposition(-199,-199)
  if john.ycor() > 200: 
    john.setposition(john.xcor(), 199)
  if john.xcor() > 200: 
    john.setposition(199, john.ycor())
  if john.ycor() < -200: 
    john.setposition(john.xcor(),-199)
  if john.xcor() < -200: 
    john.setposition(-199, john.ycor())
def move_backward():
  john.pencolor("white")
  john.backward(9)
  if john.xcor() < -200 and john.ycor()> 200:
    john.setposition(-199,199)
  if  john.xcor() > 200 and john.ycor()> 200: 
    john.setposition(199,199)
  if john.xcor() > 200 and john.ycor() < -200: 
    john.setposition(199,-199)
  if john.xcor() < -200 and john.ycor()<-200: 
    john.setposition(-199,-199)
  if john.ycor() > 200: 
    john.setposition(john.xcor(), 199)
  if john.xcor() > 200: 
    john.setposition(199, john.ycor())
  if john.ycor() < -200: 
    john.setposition(john.xcor(),-199)
  if john.xcor() < -200: 
    john.setposition(-199, john.ycor())
def turn_right():
  john.right(10)
def turn_left():
  john.left(10)

def move_forward_b():
  bob.pencolor("red")
  bob.forward(10)
  if bob.xcor() < -200 and bob.ycor()> 200:
    bob.setposition(-199,199)
  if bob.xcor() > 200 and bob.ycor()> 200: 
    bob.setposition(199,199)
  if bob.xcor() > 200 and bob.ycor() < -200: 
    bob.setposition(199,-199)
  if bob.xcor() < -200 and bob.ycor()<-200: 
    bob.setposition(-199,-199)
  if bob.ycor() > 200: 
    bob.setposition(bob.xcor(), 199)
  if bob.xcor() > 200: 
    bob.setposition(199, bob.ycor())
  if bob.ycor() < -200: 
    bob.setposition(bob.xcor(),-199)
  if bob.xcor() < -200: 
    bob.setposition(-199, bob.ycor())
def move_backward_b():
  bob.pencolor("white")
  bob.backward(10)
  if bob.xcor() < -200 and bob.ycor()> 200:
    bob.setposition(-199,199)
  if bob.xcor() > 200 and bob.ycor()> 200: 
    bob.setposition(199,199)
  if bob.xcor() > 200 and bob.ycor() < -200: 
    bob.setposition(199,-199)
  if bob.xcor() < -200 and bob.ycor()<-200: 
    bob.setposition(-199,-199)
  if bob.ycor() > 200: 
    bob.setposition(bob.xcor(), 199)
  if bob.xcor() > 200: 
    bob.setposition(199, bob.ycor())
  if bob.ycor() < -200: 
    bob.setposition(bob.xcor(),-199)
  if bob.xcor() < -200: 
    bob.setposition(-199, bob.ycor())
def turn_right_b():
  bob.right(10)
def turn_left_b():
  bob.left(10)




def clear_screen():
  john.clear()
def start_draw():
  john.pendown()
def stop_draw():
  john.penup()

screen.onkey(move_forward, "Up")
screen.onkey(move_backward, "Down")
screen.onkey(turn_right, "Right")
screen.onkey(turn_left, "Left")
screen.onkey(clear_screen, "c")
screen.onkey(stop_draw, "u")
screen.onkey(start_draw, "d")

screen.onkey(move_forward_b, "e")
screen.onkey(move_backward_b, "d")
screen.onkey(turn_right_b, "f")
screen.onkey(turn_left_b, "s")

screen.listen()

Lesson 13: Turtle and lots of ifs

posted Feb 6, 2020, 8:05 AM by Attila Lengyel

import turtle as t
john = t.Turtle()

john.shape("turtle")
john.fillcolor("blue")
john.pencolor("blue")
screen = t.Screen()

john.penup()
john.setposition(-200,200)
john.pendown()
for x in range(4):
john.fd(400)
john.right(90)
john.penup()
john.setposition(0,0)

def define_action(john):
if john.xcor() < 200 and john.ypos()> 200:
return 1
if john.xcor() > 200 and john.ypos()> 200:
return 3
if john.xcor() > 200 and john.ypos() < -200:
return 5
if john.xcor() < -200 and john.ypos()<-200:
return 7
if john.ycor() > 200: return 2
if john.xcor() > 200: return 4
if john.ycor() < -200: return 6
if john.xcor() < -200: return 8
return 9

def action(john):
item = define_action()
john.write(item, font=("Arial", 64, "normal"))
if item == 1: john.setposition(-199,199)
if item == 3: john.setposition(199,199)
if item == 5: john.setposition(199,-199)
if item == 7: john.setposition(-199,-199)
if item == 2: john.setposition(john.xcor(), 199)
if item == 4: john.setposition(199, john.ycor())
if item == 6: john.setposition(john.xcor(),-199)
if item == 8: john.setposition(-199, john.ycor())
def move_forward():
john.pencolor("blue")
john.forward(10)
def move_backward():
john.pencolor("white")
john.backward(10)
def turn_right():
john.right(10)
def turn_left():
john.left(10)
def clear_screen():
john.clear()
def start_draw():
john.pendown()
def stop_draw():
john.penup()

screen.onkey(move_forward, "Up")
screen.onkey(move_backward, "Down")
screen.onkey(turn_right, "Right")
screen.onkey(turn_left, "Left")
screen.onkey(clear_screen, "c")
screen.onkey(stop_draw, "u")
screen.onkey(start_draw, "d")

screen.listen()

Lesson 12: Controlling the Turtle

posted Jan 30, 2020, 8:00 AM by Attila Lengyel

import turtle as t
john = t.Turtle()

john.shape("turtle")
john.fillcolor("blue")
john.pencolor("blue")
screen = t.Screen()

def move_forward():
john.pencolor("blue")
john.forward(10)
def move_backward():
john.pencolor("white")
john.backward(10)
def turn_right():
john.right(10)
def turn_left():
john.left(10)
def clear_screen():
john.clear()
def start_draw():
john.pendown()
def stop_draw():
john.penup()

screen.onkey(move_forward, "Up")
screen.onkey(move_backward, "Down")
screen.onkey(turn_right, "Right")
screen.onkey(turn_left, "Left")
screen.onkey(clear_screen, "c")
screen.onkey(stop_draw, "u")
screen.onkey(start_draw, "d")

screen.listen()

Lesson 11: Python Turtle

posted Jan 16, 2020, 9:05 AM by Attila Lengyel

import turtle

bob = turtle.Turtle()
bob.forward(80)

bob.left(90)
bob.forward(80)

bob.left(90)
bob.forward(80)

bob.left(90)
bob.forward(80)

bob.clear()

bob.shape("turtle")
bob.pencolor("red")

for i in range(4):
bob.left(90)
bob.forward(80)
bob.clear()

bob.left(90)

for i in range(5):
bob.fd(80)
bob.right(144)

bob.clear()

sides = int(input("How many sides?:"))
side_length = int(input("Length?:"))

for i in range(sides):
bob.forward(side_length)
bob.right(360/sides)

Lesson 10: First 100 prime numbers

posted Jan 9, 2020, 8:00 AM by Attila Lengyel

print(10%3)
print(15%6)

n = int(input("Give me a number:"))

isprime = True
for i in range(2,n):
if n%i == 0:
isprime = False
break

if isprime:
print(n, "is a prime number")
else:
print(n, "is not a prime number")

def spoungebob(n):
isprime = True
for i in range(2,n):
if n%i == 0:
isprime = False
break
return isprime

if spoungebob(n):
print("Spounge Bob says", n, "is a prime number")
else:
print("Spounge Bob says", n, "is not a prime number")

counter = 0
n = 0
while counter <= 1000:
n = n + 1
if spoungebob(n):
print(counter, n)
counter = counter + 1





Lesson 9: Binary search

posted Dec 19, 2019, 8:02 AM by Attila Lengyel

target = int(input("Think of a number:"))
lower = 1
upper = 100

while target < 1 or target > 100:
print("Please think between 1 and 99!")
target = int(input("Try again:"))

guess = 0

import math
while guess != target:
guess = lower + math.floor((upper - lower)/2)
print("Is the number", guess, "? (yes/lower/higher):")
answer = input()
if answer == "yes":
print("I knew it!!!")
elif answer == "lower":
upper = guess - 1
elif answer == "higher":
lower = guess + 1

Lesson 8: Rolling the dice

posted Dec 12, 2019, 7:59 AM by Attila Lengyel

import random as r

print("Let's roll the dice!")
roll = r.randint(1,6)
print("I rolled ", roll)

rolls = [0,0,0,0,0,0]

for x in range(0, 100):
roll = r.randint(1,6)
rolls[roll-1] = rolls[roll-1] + 1

print(rolls)

rolls2 = [0,0,0,0,0,0,0,0,0,0,0,0]

for x in range(0,1000000):
roll1 = r.randint(1,6)
roll2 = r.randint(1,6)
num = roll1 + roll2
rolls2[num-1] = rolls2[num-1] + 1

for x in range(0,12):
print(x + 1, ":", rolls2[x])



Lesson 7: Fibonacci sequence with for loop and list

posted Dec 5, 2019, 8:55 AM by Attila Lengyel

a = 1
b = 1
c = a + b
print(a,b,c)

a = b
b = c
c = a + b
print(c)

a = b
b = c
c = a + b
print(c)

print("I'm the while loop")
x = 0
while x < 10:
a = b
b = c
c = a + b
print(c)
x = x + 1

a = 1
b = 1
c = a + b
print("I'm the for loop")
for x in range(0,10):
a = b
b = c
c = a + b
print(x, ":", c)

print("I'm the list")
f = [1,1,2]
for x in range(0,11):
f.append(f[len(f)-1] - 2 * f[len(f)-2])
print(f)

1-10 of 106