Add Chapter 6 python files and fix grammar issues in readme

This commit is contained in:
lolabird 2023-09-17 08:08:06 -06:00
commit bcb2885eeb
15 changed files with 422 additions and 3 deletions

View file

@ -0,0 +1,33 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 1
import turtle
def drawSquare(t, sz):
"""Get turtle t to draw a square of sz side"""
for i in range(4):
t.forward(sz)
t.left(90)
wn = turtle.Screen()
wn.bgcolor("lightgreen")
alex = turtle.Turtle()
alex.color("pink")
alex.pensize(2)
def drawManySquares(tt, num):
for i in range(num):
drawSquare(tt, 20)
tt.up()
tt.forward(30)
tt.down()
drawManySquares(alex, 5)
wn.exitonclick()

View file

@ -0,0 +1,34 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 10
import turtle
def drawStar(t):
for i in range(5):
t.left(144)
t.forward(-100)
def drawFiveStars(tt):
tt.up()
tt.goto(-180,50)
tt.down()
for i in range(5):
drawStar(tt)
tt.up()
tt.forward(350)
tt.right(144)
tt.down()
wn = turtle.Screen()
wn.bgcolor("tan")
stella = turtle.Turtle()
stella.color("purple")
stella.pensize(2)
stella.speed(10)
drawFiveStars(stella)
wn.exitonclick()

View file

@ -0,0 +1,39 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 11
import turtle
def drawStar(t, numSides, sideLength):
angle = 180 - 180/numSides
t.up()
t.goto(sideLength/2,0)
t.down()
for i in range(numSides):
t.left(angle)
t.forward(sideLength)
def checkSides(tt, sides, length):
if sides >= 3 and sides % 2 != 0:
print("What a beautiful star!")
drawStar(tt, sides, length)
else:
print("Oops! Your star must have an ODD number of at least 3 sides. Let's try that again!")
wn = turtle.Screen()
wn.bgcolor("tan")
stella = turtle.Turtle()
stella.color("purple")
stella.pensize(2)
stella.speed(0)
a = int(input("Help Stella draw a star! How many points does the star have?"))
b = int(input("How long is each side?"))
checkSides(stella, a, b)
wn.exitonclick()

View file

@ -0,0 +1,28 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 12
import turtle
def drawSprite(t, numSides, legLength):
angle = 360/numSides
for i in range(numSides):
t.forward(legLength)
t.up()
t.forward(-legLength)
t.right(angle)
t.down()
wn = turtle.Screen()
wn.bgcolor("tan")
avery = turtle.Turtle()
avery.color("brown")
drawSprite(avery, 15, 120)
wn.exitonclick()

View file

@ -0,0 +1,14 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 13
def sumTo(n):
# your code here
runningTotal = 0
for counter in range(n):
runningTotal = runningTotal + (n+1)/2
return runningTotal
print(sumTo(10))

View file

@ -0,0 +1,14 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 14
def mySqrt(n):
# your code here
oldGuess = n/2
while oldGuess**2 != n:
oldGuess = (1/2)*(oldGuess+(n/oldGuess))
return oldGuess
print(mySqrt(10000))

View file

@ -0,0 +1,18 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 15
def myPi(iters):
# Calculate an approximation of PI using the Leibniz
# approximation with iters number of iterations
# your code here
apprPi = 0
for i in range(iters):
term = (-1)**i/(2*i+1)
apprPi += term
return apprPi * 4
print(myPi(1000000))

View file

@ -0,0 +1,20 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 16
import math
def myPi(iters):
# Calculate an approximation of PI using the Madhava
# approximation with iters number of iterations
#your code here
apprPi = 0
for i in range(iters):
term = (-1)**i/((2*i+1)*3**i)
apprPi += term
return apprPi * math.sqrt(12)
print(myPi(10))

View file

@ -0,0 +1,32 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 17
import turtle
def drawSprite(t, numSides, legLength):
angle = 360/numSides
for i in range(numSides):
t.forward(legLength)
t.up()
t.forward(-legLength)
t.right(angle)
t.down()
def fancySquare(tt, sz):
"""Get turtle t to draw a square of sz side"""
for i in range(4):
drawSprite(tt, int(sz/10), int(sz/20))
tt.forward(sz)
tt.left(90)
wn = turtle.Screen()
wn.bgcolor("lightblue")
alex = turtle.Turtle()
alex.color("maroon")
alex.pensize(2)
fancySquare(alex, 100)

View file

@ -0,0 +1,56 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 18
import turtle
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")
tess = turtle.Turtle() # create tess and set some attributes
tess.color("purple")
tess.fillcolor("magenta")
tess.pensize(3)
alex = turtle.Turtle()
alex.color("magenta")
for a in xs:
drawBar(tess, a)
writeData(alex, a)
wn.exitonclick()

View file

@ -0,0 +1,36 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 2
import turtle
def drawSquare(t, sz):
"""Get turtle t to draw a square of sz side"""
for i in range(4):
t.forward(sz)
t.left(90)
wn = turtle.Screen()
wn.bgcolor("lightgreen")
alex = turtle.Turtle()
alex.color("pink")
alex.pensize(2)
def drawStackedSquares(tt, num):
for i in range(num):
drawSquare(tt, 20*(i+1))
tt.up()
tt.backward(10)
tt.left(90)
tt.backward(10)
tt.right(90)
tt.down()
drawStackedSquares(alex, 5)
wn.exitonclick()

View file

@ -0,0 +1,25 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 3
import turtle
def drawPolygon(t, sideLength, numSides):
"""Make turtle t draw a polygon."""
angle = 360/numSides
for i in range(numSides):
t.forward(sideLength)
t.left(angle)
wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
tess.color("pink")
tess.pensize(2)
drawPolygon(tess,50,8)
wn.exitonclick()

View file

@ -0,0 +1,32 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 4
import turtle
def drawSquare(t, sz):
"""Get turtle t to draw a square of sz side"""
for i in range(4):
t.forward(sz)
t.left(90)
wn = turtle.Screen()
wn.bgcolor("lightgreen")
alex = turtle.Turtle()
alex.color("blue")
alex.pensize(2)
alex.speed(10)
def drawCircleSquares(tt, num):
for i in range(num):
drawSquare(tt, 80)
tt.right(15)
drawCircleSquares(alex, 25)
wn.exitonclick()

View file

@ -0,0 +1,38 @@
# Runestone.Academy thinkcspy course
# Chapter 6
# Problem 5
import turtle
def drawSquareSpiral(t, sz, num, angle):
"""Get turtle t to draw a square of sz side"""
for i in range(num):
t.forward(sz*i)
t.left(angle)
wn = turtle.Screen()
wn.setworldcoordinates(-300,-300,300,300)
wn.bgcolor("lightgreen")
alex = turtle.Turtle()
alex.color("blue")
alex.speed(0)
alex.up()
alex.goto(-150,0)
alex.down()
tess = turtle.Turtle()
tess.color("purple")
tess.speed(0)
tess.up()
tess.goto(150,0)
tess.down()
drawSquareSpiral(alex,2,100,90)
drawSquareSpiral(tess,2,100,91)
wn.exitonclick()