Bronze Announcements


Lesson 19: Bar charts with Turtle

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

import random
results = [0,0,0,0,0,0,0,0,0,0,0,0]

for x in range(10000):
dice = random.randint(1,6)
dice2 = random.randint(1,6)
results[dice + dice2 -1] = results[dice + dice2-1] + 1

import turtle as t
nike = t.Turtle()
nike.penup()
nike.setposition(-200,-200)
nike.pendown()
nike.fd(400)
nike.penup()
nike.setposition(-200,-200)
nike.pendown()
nike.left(90)
nike.fd(400)
x_start = -230
for r in results:
x_start = x_start + 40
nike.penup()
nike.setposition(x_start,-200)
nike.pendown()
height = (400 * r)/2000
nike.begin_fill()
nike.fd(height)
nike.write(r)
nike.right(90)
nike.fd(20)
nike.right(90)
nike.fd(height)
nike.end_fill()
nike.left(180)


Lesson 18: Pixels taught by Parag Gupta

posted Mar 4, 2020, 1:49 PM by Attila Lengyel   [ updated Mar 4, 2020, 1:54 PM ]

#### create a drawing area filled with pixels whose colors can be changed by clicking
# initialize some stuff and create a function to draw a square
import turtle as t
goo = t.Turtle()
goo.speed(0)
def draw_square (side_len):
for i in range(4):
goo.forward(side_len)
goo.left(90)
### draw the boundary square starting from (-300, -300) with each side being 600
sq_x = -300 ## starting x coordinate of square
sq_y = -300 ## starting y coordinate of square
sq_sz = 600 ## length of each side of square
goo.penup()
goo.setposition(sq_x, sq_y)
goo.pendown()
draw_square(sq_sz)
### draw the grid of pixels
row_pixels = 8 #### adjust this to change resolution of pixels ####
pixel_width = sq_sz/row_pixels ## length of each side of the pixel
def draw_grid (row_pix):
for i in range (row_pix):
# draw a row of pixels
for j in range (row_pix):
draw_square(pixel_width)
goo.forward(pixel_width)
# go to the beginning of the next row of pixels
goo.penup()
goo.setposition(sq_x, goo.ycor()+pixel_width)
goo.pendown()
# comment the following out if you don't need the grid drawn since
# it can take some time to draw
draw_grid (row_pixels)
### create a list of colors, feel free to add to or remove from the list
colors = ['black', 'white', 'red', 'gray', 'orange', 'magenta', 'green', 'blue', 'yellow', 'brown']
num_colors = len(colors)
### Figure out the total number of pixels and create a list having an entry for
### each pixel which keeps track of the next color when that pixel is clicked. Initialize
### each entry to 0, which means the first color would be black. For example, if
### a particular entry had a value of 3, that pixel would be gray when clicked next. When
### a pixel gets clicked on, we would need to increment the value for that pixel in this list.
num_pixels = row_pixels * row_pixels
# initialize the list called pix_color to keep track of colors for each pixel
pix_color = []
# set each value in the list to initially be 0
for i in range(num_pixels) :
pix_color.append(0)
### Define a function to fill the clicked pixel with a color, where cl_x and cl_y
### are the x and y coordinates of the clicked location. The bottom left pixel is pixel 0.
### The one to the right is pixel 1 and so forth. If row_pixels was 4, the first pixel
### in the second row would be pixel 4 and the top right pixel would be pixel 15. We
### use the pixel numbers to keep track of what color each pixel will be when clicked next.
def fill_pixel(cl_x, cl_y):
# only fill the pixel if the click was within the big square
if ((cl_x > sq_x) and (cl_x < (sq_x + sq_sz)) and
(cl_y > sq_y) and (cl_y < (sq_y + sq_sz))) :
## figure out the pixel number
# calculate the x and y coordinates within our square and then divide by
# the pixel width to figure out which row and column the click was in
x = cl_x - sq_x
pix_col = int(x / pixel_width)
y = cl_y - sq_y
pix_row = int(y / pixel_width)
# calculate the actual pixel number from the row and column numbers
pix_num = pix_col + (pix_row * row_pixels)
print (pix_num)
# Calculate the start coordinates of the pixel to fill by using the column,
# row, pixel width, and square coordinates.
fill_pix_x = (pix_col * pixel_width) + sq_x
fill_pix_y = (pix_row * pixel_width) + sq_y
# Figure out the fill color by looking up the pixel number in the pix_color list
# and then using the value in that list to look up the actual color in the colors list.
fill_color = colors[pix_color[pix_num]]
goo.color(fill_color)
# Increment the value for that pixel in the pix_color list. Use the mod function so
# value goes back to 0 when we've reached the last color.
pix_color[pix_num] = (pix_color[pix_num] + 1) % num_colors
# fill the pixel with the fill color
goo.penup()
goo.setposition(fill_pix_x, fill_pix_y)
goo.begin_fill()
draw_square(pixel_width)
goo.end_fill()
## When the screen is clicked, fill the pixel, and wait for the next click.
screen = t.Screen()
screen.onscreenclick(fill_pixel)
screen.listen()


Lesson 17: Practice makes master

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

a = int(input("Give me a number:"))
b = int(input("Give me an other:"))
print(a + b)

print("\n\n First 100 numbers")
num = []
for i in range(0,101):
num.append(i)
print(num)

print("\n\n\nEven numbers only:")
num.clear();
for i in range(0,101):
if i%2 == 0:
num.append(i)
print(num)

import turtle as t
bob = t.Turtle()
for i in range(0,4):
bob.forward(100)
bob.left(90)

Lesson 16: Nested loops with Turtle

posted Feb 11, 2020, 8:01 AM by Attila Lengyel   [ updated Feb 11, 2020, 8:02 AM ]

import turtle as t
import random as r

goo = t.Turtle()
goo.speed(0)
colors = ['red', 'orange', 'magenta', 'green', 'blue', 'yellow']

for k in range(0,8):
for i in range(0,8):
x = -180 + i * 50
y = 180 - k * 50
goo.penup()
goo.setposition(x,y)
goo.pendown()
goo.color(r.choice(colors))
goo.begin_fill()
for j in range(0,4):
goo.right(90)
goo.forward(40)
goo.end_fill()



for i in range(0,5):
for j in range(0,5):
print(i, "x", j, "=", i * j)



Lesson 15: Turtle in the box

posted Feb 4, 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()

john.penup()
john.setposition(-200,200)
john.pendown()
john.forward(400)
john.right(90)
john.forward(400)
john.right(90)
john.forward(400)
john.right(90)
john.forward(400)
john.setposition(0,0)

def geofence():
if john.ycor() > 190: return False
if john.ycor() < -190: return False
if john.xcor() > 190: return False
if john.xcor() < -190: return False
return True

def move_forward():
speed = 10
if geofence() == False :
john.setposition(0,0)
john.pencolor("blue")
john.forward(speed)
def move_backward():
speed = 10
if geofence() == False :
john.setposition(0,0)
john.pencolor("white")
john.backward(speed)
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 14: Controlling the turtle

posted Jan 28, 2020, 8:03 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 13: Drawing letter S with Turtle

posted Jan 21, 2020, 10:08 AM by Attila Lengyel

import turtle
def draw_s(t, x, y):
min_y = 100000
max_y = -100000
t.penup()
t.setpos(x, y)
t.pendown()
t.left(90)

for i in range(27):
t.left(10)
t.forward(1)
if t.ycor() < min_y:
min_y = t.ycor()
if t.ycor() > max_y:
max_y = t.ycor()

for i in range(27):
t.right(10)
t.forward(1)
if t.ycor() < min_y:
min_y = t.ycor()
if t.ycor() > max_y:
max_y = t.ycor()
print(min_y, max_y)

bob = turtle.Turtle()
draw_s(bob, -130,130)

Lesson 12: Turtle is here... simple drawing with Python Turtle

posted Jan 14, 2020, 8:05 AM by Attila Lengyel

import turtle as t

jojo_siwa = t.Turtle()

jojo_siwa.forward(50)
jojo_siwa.right(90)

jojo_siwa.forward(50)
jojo_siwa.right(90)

jojo_siwa.forward(50)
jojo_siwa.right(90)

jojo_siwa.forward(50)
jojo_siwa.right(90)


for i in range (0,4):
jojo_siwa.forward(50)
jojo_siwa.left(90)

jojo_siwa.clear()

for i in range(0,5):
jojo_siwa.forward(50)
jojo_siwa.right(144)

jojo_siwa.clear()

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

angle = 360 / num_sides
for i in range(0,num_sides):
jojo_siwa.forward(side_length)
jojo_siwa.right(angle)


jojo_siwa.done()


Lesson 11: Prime numbers and functions

posted Jan 7, 2020, 8:01 AM by Attila Lengyel

print(10%2)

for i in range(1,10):
print("10 modulo", i, "is", 10%i)


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

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

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

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

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

for i in range (1,1000):
if isprime(i):
print(i)

Lesson 10: Binary search

posted Dec 17, 2019, 8:16 AM by Attila Lengyel

upper = int(input("Give me the max:"))
lower = int(input("Give me the min:"))

guess = int(input("Guess a number in between:"))

while guess > upper or guess < lower:
print("Wrong number! Try again.")
guess = int(input("Guess a number in between:"))


import math

print(math.ceil(34.7))
print(math.floor(34.7))

xyx = math.floor((upper + lower)/2)
while xyx != guess:
print("Is it", xyx,"?")
answer= input("yes or no?:")
if answer == "no":
answer = input("Is it lower or higher?")
print(answer)
if answer == "lower":
upper = xyx
else:
lower = xyx
xyx = math.floor((upper + lower)/2)


1-10 of 64