Silver Announcements


Lesson 22: Draw THANK with turtle

posted Jun 7, 2019, 8:07 AM by Attila Lengyel

import turtle as t

tt = t.Turtle()

def draw_t(bob,x,y):
bob.penup()
bob.goto(x,y)
bob.pendown()
bob.forward(50)
bob.left(180)
bob.forward(25)
bob.left(90)
bob.forward(60)
bob.left(90)

def draw_h(bob,x,y):
bob.penup()
bob.goto(x,y)
bob.pendown()
bob.right(90)
bob.forward(60)
bob.left(180)
bob.forward(30)
bob.right(90)
bob.forward(30)
bob.left(90)
bob.forward(30)
bob.left(180)
bob.forward(60)
bob.left(90)

def draw_a(bob, x,y):
bob.penup()
bob.goto(x,y)
bob.pendown()
bob.goto(x + 15, y + 60)
bob.goto(x + 30, y + 0)
bob.penup()
bob.goto(x,y + 30)
bob.pendown()
bob.goto(x + 30, y + 30)

def draw_n(bob, x, y):
bob.penup()
bob.goto(x,y)
bob.pendown()
bob.left(90)
bob.forward(60)
bob.goto(x + 30, y)
bob.forward(60)
bob.right(90)

def draw_k(bob, x, y):
bob.penup()
bob.goto(x,y)
bob.pendown()
bob.right(90)
bob.forward(60)
bob.right(180)
bob.forward(25)
bob.goto(x + 30 ,y)
bob.goto(x,y -35)
bob.goto(x + 30 ,y - 60)
draw_t(tt, -200, 200)
draw_h(tt, -140, 200)
draw_a(tt, -100, 140)
draw_n(tt, -60, 140)
draw_k(tt, -20, 200)

Lesson 21: Turtle in corners

posted May 31, 2019, 8:00 AM by Attila Lengyel

import turtle
import time
import random

t = turtle.Turtle()

def draw(x,y, delay, s):
t.clear()
t.penup()
t.goto(x,y)
t.pendown()
t.speed(s)
for x in range(0,3):
t.forward(50)
t.left(120)
time.sleep(delay)

time.sleep(2)
draw(0,0,3, 5)
for x in range(0,10):
time.sleep(1)
corner = random.randint(1,4)
if corner == 1:
draw(200,200,0,0)
elif corner == 2:
draw(-200, 200,0,0)
elif corner == 3:
draw(-200, -200,0,0)
else:
draw(200, -200,0,0)
time.sleep(2)
draw(0,0,3)

Lesson 20: Catch me if you can

posted May 10, 2019, 7:41 AM by Attila Lengyel   [ updated May 10, 2019, 8:04 AM ]

import turtle as t

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

player2 = t.Turtle()
player2.shape("turtle")
player2.fillcolor("red")
player2.pencolor("white")
player2.goto(-180, 180)

speed = 10
angle = 10
screen = t.Screen()

def draw_rectangle(player, x, y, width, height, color):
startpos = player.pos()
startcolor = player.pencolor()
player.pencolor(color)
player.up()
player.goto(x, y)
player.down()
player.forward(width)
player.left(90)
player.forward(height)
player.left(90)
player.forward(width)
player.left(90)
player.forward(height)
player.left(90)
player.up()
player.goto(startpos)
player.down()
player.pencolor(startcolor)
draw_rectangle(player1, -210, -210, 420, 420, "black")

def position(player):
if player.xcor() >= 200 :
player.up()
player.goto(199, player.ycor())
player.down()
return False
if player.xcor() <= -200:
player.up()
player.goto(-199, player.ycor())
player.down()
return False
if player.ycor() >= 200:
player.up()
player.goto(player.xcor(), 199)
player.down()
return False
if player.ycor() <= -200:
player.up()
player.goto(player.xcor(), -199)
player.down()
return False
return True

import math
def catch(playera, playerb):
x1 = playera.xcor()
y1 = playera.ycor()
x2 = playerb.xcor()
y2 = playerb.ycor()

c = math.pow(x1-x2,2) + math.pow(y2-y1,2)
c = math.sqrt(c)
if c < 10:
playera.fillcolor("red")
playerb.fillcolor("blue")
def f():
if position(player1): player1.forward(speed)
def b():
if position(player1): player1.backward(speed)
def r(): player1.right(angle)
def l(): player1.left(angle)

screen.onkey(f, "Up")
screen.onkey(b, "Down")
screen.onkey(r, "Right")
screen.onkey(l, "Left")

def f2():
if position(player2): player2.forward(speed)
catch(player1, player2)
def b2():
if position(player2): player2.backward(speed)
catch(player1, player2)
def r2(): player2.right(angle)
def l2(): player2.left(angle)

screen.onkey(f2, "q")
screen.onkey(b2, "a")
screen.onkey(r2, "x")
screen.onkey(l2, "z")
screen.listen()

Lesson 18: Turtle in a box

posted May 3, 2019, 8:45 AM by Attila Lengyel   [ updated May 10, 2019, 7:28 AM ]

import turtle as t

player1 = t.Turtle()
player1.shape("turtle")
player1.pencolor("white")

speed = 10
angle = 10
screen = t.Screen()

def draw_rectangle(player, x, y, width, height, color):
startpos = player.pos()
startcolor = player.pencolor()
player.pencolor(color)
player.up()
player.goto(x, y)
player.down()
player.forward(width)
player.left(90)
player.forward(height)
player.left(90)
player.forward(width)
player.left(90)
player.forward(height)
player.left(90)
player.up()
player.goto(startpos)
player.down()
player.pencolor(startcolor)

draw_rectangle(player1, -210, -210, 420, 420, "red")

def position(player):
if player.xcor() >= 200 :
player.up()
player.goto(199, player.ycor())
player.down()
    return False
if player.xcor() <= -200:
player.up()
player.goto(-199, player.ycor())
player.down()
    return False
if player.ycor() >= 200:
player.up()
player.goto(player.xcor(), 199)
player.down()
    return False
if player.ycor() <= -200:
player.up()
player.goto(player.xcor(), -199)
player.down()
    return False
return True

def f():
if position(player1): player1.forward(speed)
def b():
if position(player1): player1.backward(speed)
def r(): player1.right(angle)
def l(): player1.left(angle)

screen.onkey(f, "Up")
screen.onkey(b, "Down")
screen.onkey(r, "Right")
screen.onkey(l, "Left")

screen.listen()

Lesson 17: Drawing with Turtle

posted Apr 12, 2019, 8:08 AM by Attila Lengyel   [ updated Apr 12, 2019, 8:12 AM ]

import turtle

bob = turtle.Turtle()

bob.forward(50)
bob.left(90)
bob.forward(50)

bob.left(90)
bob.forward(50)

bob.left(90)
bob.forward(50)

bob.clear()

for i in range(4):
bob.forward(100)
bob.right(90)

bob.clear()

bob.pencolor("red")

num_side = 6
side_length = 100
angle = 360 / num_side

for i in range(num_side):
bob.forward(side_length)
bob.right(angle)

bob.clear()

for i in range(5):
if i == 1 or i == 3: bob.penup()
bob.forward(50)
bob.right(144)
if i == 1 or i == 3: bob.pendown()

bob.clear()

for i in range(10):
bob.forward(i * 20)
bob.right(144)

bob.clear()
bob.penup()
bob.setposition(0,0)
bob.pendown()

bob.speed(2)
colors = ['red', 'purple', 'blue', 'green', 'orange', 'yellow']
for x in range(100):
bob.pencolor(colors[x%6])
bob.circle(5*x)
bob.circle(-5*x)
bob.left(x)


Lesson 16: What is a palindrome?

posted Apr 5, 2019, 8:04 AM by Attila Lengyel

def eliminate(w):
tmp = ""
for i in w:
if i.isalnum():
tmp = tmp + i.lower()

return tmp

word = input("Give me a word:")
word = eliminate(word)

reverse = ""
for i in word:
reverse = i + reverse
print(reverse)

if word == reverse:
print("This is a palindrome")
else:
print("This is not a palindrome")


Lesson 15: Hangman v1

posted Mar 29, 2019, 8:00 AM by Attila Lengyel

cl = ""
wl = " "
def draw(word, cl, wl):
a = ""
for c in word:
if c in cl:
a += c + " "
else:
a += "_ "
print(a)
if "_" not in a:
return "win"
elif len(wl) > 7:
return "lost"
else:
return "next"
word = input("Give me a word:")
status = draw(word, cl, wl)
while status == "next":
g = input("Guess a letter:")
if g in word and g not in cl:
cl += g
elif g not in wl:
wl += g
status = draw(word, cl, wl)
if status != "next":
print(status)

Lesson 14: Odd, even and prime numbers with modulo

posted Mar 22, 2019, 7:56 AM by Attila Lengyel

import random as r

number = r.randint(0,1000)

if number % 2 == 0:
print(number, "is an even number")
else:
print(number, "is odd number")

#list up prime numbers
prime = [2,3,5,7,11]
while len(prime) < 1000:
nextnum = prime[len(prime) - 1]
stop = False

while stop == False:
nextnum += 1
isPrime = True
for i in prime:
if nextnum % i == 0:
isPrime = False
break
if isPrime:
prime.append(nextnum)
stop = True
print(prime)

Number guessing game user vs computer

posted Mar 15, 2019, 11:03 AM by Attila Lengyel

def guessgame(guess, n):
if guess == n:
return 0
elif guess < n:
return -1
else:
return 1

import random as r
n = r.randint(0,100)
counter = 0

guessed = False
while (guessed == False):
guess = int(input("What number did I think of?:"))
counter += 1
result = guessgame(guess, n)
if result == 0:
guessed = True
elif result == -1:
print("Too low")
else:
print("Too high")

print("Good guess! The number is", n, " You tried", counter, "times")

import math

print("Now you think of a number between 0 and 100!")
upper = 100
lower = 0
answer =""

while answer != "good":
guess = lower + math.floor((upper - lower)/2)
print("Is it", guess, "? (high/low/good):")
answer = input()

if answer == "high":
upper = guess
elif answer == "low":
lower = guess
else:
print("I made it!!! The number is", guess)


Scrambled words: the rset can be a toatl mses and you can sitll raed it wouthit porbelm

posted Feb 15, 2019, 8:05 AM by Attila Lengyel

import random
def randomize(word):
if len(word) < 3:
return word
chars = []
for i in word: #this stores every characters in a list
chars.append(i)
result =[]
result.append(chars[0]) # stores the first character
del chars[0] #deletes from the list
result.append(chars[len(chars)-1]) # stores the last character
del chars[len(chars)-1] #delets the last character
while len(chars) != 0:
r = random.randint(0,len(chars)-1)
result.insert(1, chars[r]) #inserts into the second position
del chars[r]
randomword = ""
for i in result: #appends the random list of characters
randomword += i
return randomword #returns randomword

text = "the rest can be a total mess and you can still read it without problem"
sentance = ""
for i in text.split(" "):
sentance += randomize(i) + " "
print(sentance)

1-10 of 91