import turtle
import random
= 300
MAX_COORD = ['yellow', 'blue', 'red', 'green']
COLORS = 25
RADIUS
= turtle.Turtle()
t
for i in range(20): # repeat
0,3)])
t.fillcolor(COLORS[random.randint(
t.penup()-MAX_COORD,MAX_COORD), random.randint(-MAX_COORD,MAX_COORD))
t.goto(random.randint(
t.pendown()
t.begin_fill()
t.circle(RADIUS) t.end_fill()
Class demonstrations
Random circles in Turtle Grapics
Are you a horse?
import turtle
= turtle.Turtle()
t
= input("Choose between square, circle or both: ")
choice if choice.lower() == "square" or choice.lower() == "both":
100)
turtle.forward(90)
turtle.right(100)
turtle.forward(90)
turtle.right(100)
turtle.forward(90)
turtle.right(100)
turtle.forward(if choice.lower() == "circle" or choice.lower() == "both":
50)
turtle.circle(
print("Finished!")
Dog class
class Dog(): # definition of the class ("blueprint")
= 20
max_age
def __init__(self, breed, age, color):
self.breed = breed
self.age = age
self.color = color
def eat(self): # a method (NOTICE THAT IT IS INSIDE THE CLASS DEFINITION)
pass # using "pass" to write it as an empty function (to be filled later)
def sleep(self):
pass
def sit(self):
pass
def average_color(c1, c2):
return c1 + ' and ' + c2
def crossbreed(self, dog):
if self.breed == dog.breed:
= Dog(self.breed,0,Dog.average_color(self.color, dog.color))
puppy else:
= Dog('mutt',0,Dog.average_color(self.color, dog.color))
puppy return puppy
def __str__(self):
return f'breed: {self.breed}, age: {self.age}, color: {self.color}'
# testing
= Dog('pug', 3, 'black')
a = Dog('boxer', 2, 'white')
b print(a.crossbreed(b))
breed: mutt, age: 0, color: black and white
Bridge game
import random
class Game():
def __init__(self, deck, teams):
self.deck = deck
self.teams = teams
self.currentTurn = None
def start(self):
for t in self.teams:
= 0
t.score self.deck.shuffle()
= len(self.deck.cards)//4 # 4 players
deal_number # deal cards
for t in self.teams:
for p in t.players:
for i in range(deal_number):
self.deck.deal())
p.hand.append(
def end(self):
pass
class Team():
def __init__(self, players, score=0):
self.players = players
self.score = score
def addScore(self, points):
self.score += points
def __str__(self):
= 'Team: ' + '/'.join([p.name for p in self.players]) + '\n'
s for p in self.players:
+= str(p)
s return s
class Player():
def __init__(self, name, hand=[], score=0):
self.hand = hand.copy() # needs to copy! otherwise it will be the same list for all players
self.name = name
self.score = score
def playCard(self,card):
if self.hand.index(card):
= self.hand.pop(self.hand.index(card))
played print(self.name + ' played a ' + str(played))
def __str__(self):
= f'{self.name} has {self.score} points. Hand is:\n'
s for i in self.hand:
+= str(i)+'\n'
s += '\n'
s return s
class Card():
= ['spades', 'clubs', 'hearts', 'diamonds']
suits = ['A','2','3','4','5','6','7','8','9','10','Q','J','K']
ranks
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return self.rank + ' of ' + self.suit
class Deck():
def __init__(self):
= []
c for s in Card.suits:
for r in Card.ranks:
c.append(Card(r, s))self.cards = c
def shuffle(self):
self.cards)
random.shuffle(
def deal(self):
return self.cards.pop()
def __str__(self):
= 'Deck: '
s for i in self.cards:
+= str(i)+' '
s += '\n'
s return s
= Player('Bob')
bob = Player('Jen')
jen = Player('John')
john = Player('Mary')
mary = Team((bob, jen))
team1 = Team((john, mary))
team2 = Deck()
d = Game(d, (team1, team2))
g
g.start()print(team1)
print(team2)
Shapes and areas
import math
class Shape:
def area(self):
return 0
def __eq__(self, other):
return self.area() == other.area()
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius**2
# main chunk
= Rectangle(5, 10)
rectangle = Circle(3) circle
Reading a text file with names
= open('names.txt', 'r')
names_file = []
names for i in range(100):
= names_file.readline()
complete_name ' '))
names.append(complete_name.split(for n in names:
print(n[1])
names_file.close()