Add Chapter 7 python files
This commit is contained in:
parent
5c38f5ef86
commit
de0699ead3
3 changed files with 175 additions and 0 deletions
41
thinkcspy/Chapter-7-Exercises/Python-files/Problem13.py
Normal file
41
thinkcspy/Chapter-7-Exercises/Python-files/Problem13.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
# Runestone.Academy thinkcspy course
|
||||||
|
# Chapter 7
|
||||||
|
# Problem 13
|
||||||
|
|
||||||
|
def isLeap(year):
|
||||||
|
# your code here
|
||||||
|
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def easter(year):
|
||||||
|
a = year % 19
|
||||||
|
b = year % 4
|
||||||
|
c = year % 7
|
||||||
|
d = (19 * a + 24) % 30
|
||||||
|
e = (2 * b + 4 * c + 6 * d + 5) % 7
|
||||||
|
|
||||||
|
if year >= 1900 and year <= 2099:
|
||||||
|
if isLeap(year):
|
||||||
|
dateofeaster = 22 + d + e - 7
|
||||||
|
else:
|
||||||
|
dateofeaster = 22 + d + e
|
||||||
|
else:
|
||||||
|
dateofeaster = 0
|
||||||
|
|
||||||
|
return dateofeaster
|
||||||
|
|
||||||
|
def convertDate(year):
|
||||||
|
date = easter(year)
|
||||||
|
|
||||||
|
if date == 0:
|
||||||
|
print("Error: Date is out of range.")
|
||||||
|
elif date <= 30:
|
||||||
|
print("In", year, "Easter falls on March", date - 1, ".")
|
||||||
|
else:
|
||||||
|
print("In", year, "Easter falls on April", date - 31, ".")
|
||||||
|
|
||||||
|
userYear = int(input("Enter a year between 1900 and 2099."))
|
||||||
|
|
||||||
|
convertDate(userYear)
|
||||||
64
thinkcspy/Chapter-7-Exercises/Python-files/Problem4.py
Normal file
64
thinkcspy/Chapter-7-Exercises/Python-files/Problem4.py
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
# Runestone.Academy thinkcspy course
|
||||||
|
# Chapter 7
|
||||||
|
# Problem 4
|
||||||
|
|
||||||
|
import turtle
|
||||||
|
|
||||||
|
def getColor(tf, tw, height):
|
||||||
|
if height >= 200:
|
||||||
|
tf.fillcolor("red")
|
||||||
|
elif height >= 100:
|
||||||
|
tf.fillcolor("yellow")
|
||||||
|
else:
|
||||||
|
tf.fillcolor("green")
|
||||||
|
|
||||||
|
def writeData(tt, height):
|
||||||
|
tt.up()
|
||||||
|
tt.left(90)
|
||||||
|
tt.forward(height+10)
|
||||||
|
tt.right(90)
|
||||||
|
tt.forward(10)
|
||||||
|
tt.write(str(height))
|
||||||
|
tt.forward(30)
|
||||||
|
tt.right(90)
|
||||||
|
tt.forward(height+10)
|
||||||
|
tt.left(90)
|
||||||
|
|
||||||
|
|
||||||
|
def drawBar(t, height):
|
||||||
|
""" Get turtle t to draw one bar, of height. """
|
||||||
|
t.begin_fill() # start filling this shape
|
||||||
|
t.left(90)
|
||||||
|
t.forward(height)
|
||||||
|
t.right(90)
|
||||||
|
t.forward(40)
|
||||||
|
t.right(90)
|
||||||
|
t.forward(height)
|
||||||
|
t.left(90)
|
||||||
|
t.end_fill() # stop filling this shape
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
xs = [48, 117, 200, 240, 160, 260, 220] # here is the data
|
||||||
|
maxheight = max(xs)+10
|
||||||
|
numbars = len(xs)
|
||||||
|
border = 10
|
||||||
|
|
||||||
|
wn = turtle.Screen() # Set up the window and its attributes
|
||||||
|
wn.setworldcoordinates(0-border, 0-border, 40*numbars+border, maxheight+border)
|
||||||
|
wn.bgcolor("lightblue")
|
||||||
|
wn.tracer(100)
|
||||||
|
|
||||||
|
tess = turtle.Turtle() # create tess and set some attributes
|
||||||
|
tess.color("black")
|
||||||
|
tess.pensize(3)
|
||||||
|
|
||||||
|
alex = turtle.Turtle()
|
||||||
|
|
||||||
|
for a in xs:
|
||||||
|
getColor(tess, alex, a)
|
||||||
|
drawBar(tess, a)
|
||||||
|
writeData(alex, a)
|
||||||
|
|
||||||
|
|
||||||
|
wn.exitonclick()
|
||||||
70
thinkcspy/Chapter-7-Exercises/Python-files/Problem5.py
Normal file
70
thinkcspy/Chapter-7-Exercises/Python-files/Problem5.py
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
# Runestone.Academy thinkcspy course
|
||||||
|
# Chapter 7
|
||||||
|
# Problem 5
|
||||||
|
|
||||||
|
import turtle
|
||||||
|
|
||||||
|
def getColor(tf, tw, height):
|
||||||
|
if height >= 200:
|
||||||
|
tf.fillcolor("red")
|
||||||
|
elif height >= 100:
|
||||||
|
tf.fillcolor("yellow")
|
||||||
|
else:
|
||||||
|
tf.fillcolor("green")
|
||||||
|
|
||||||
|
def writeData(tt, height):
|
||||||
|
if height < 0:
|
||||||
|
writeHeight = 0
|
||||||
|
else:
|
||||||
|
writeHeight = height
|
||||||
|
|
||||||
|
tt.up()
|
||||||
|
tt.left(90)
|
||||||
|
tt.forward(writeHeight+10)
|
||||||
|
tt.right(90)
|
||||||
|
tt.forward(10)
|
||||||
|
tt.write(str(height))
|
||||||
|
tt.forward(30)
|
||||||
|
tt.right(90)
|
||||||
|
tt.forward(writeHeight+10)
|
||||||
|
tt.left(90)
|
||||||
|
|
||||||
|
|
||||||
|
def drawBar(t, height):
|
||||||
|
""" Get turtle t to draw one bar, of height. """
|
||||||
|
t.begin_fill() # start filling this shape
|
||||||
|
t.left(90)
|
||||||
|
t.forward(height)
|
||||||
|
t.right(90)
|
||||||
|
t.forward(40)
|
||||||
|
t.right(90)
|
||||||
|
t.forward(height)
|
||||||
|
t.left(90)
|
||||||
|
t.end_fill() # stop filling this shape
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
xs = [48, 117, -200, 240, 160, -260, 220] # here is the data
|
||||||
|
maxheight = max(xs)+10
|
||||||
|
minheight = min(xs)-10
|
||||||
|
numbars = len(xs)
|
||||||
|
border = 10
|
||||||
|
|
||||||
|
wn = turtle.Screen() # Set up the window and its attributes
|
||||||
|
wn.setworldcoordinates(0-border, 0+minheight, 40*numbars+border, maxheight+border)
|
||||||
|
wn.bgcolor("lightblue")
|
||||||
|
wn.tracer(100)
|
||||||
|
|
||||||
|
tess = turtle.Turtle() # create tess and set some attributes
|
||||||
|
tess.color("black")
|
||||||
|
tess.pensize(3)
|
||||||
|
|
||||||
|
alex = turtle.Turtle()
|
||||||
|
|
||||||
|
for a in xs:
|
||||||
|
getColor(tess, alex, a)
|
||||||
|
drawBar(tess, a)
|
||||||
|
writeData(alex, a)
|
||||||
|
|
||||||
|
|
||||||
|
wn.exitonclick()
|
||||||
Loading…
Reference in a new issue