Code Solutions

Lesson 2: Hexagon Spiral
import turtle
window = turtle.Screen()
window.title("Spiral")
window.setup(300, 200)
window.bgcolor('black')
t = turtle.Turtle()
t.pencolor('white')
t.speed(0)
t.width(3)
for x in range(50):
t.forward(x + 1)
t.left(66)
turtle.mainloop()
Lesson 3: Greeting Program
print("Hello, I am a program.")
name = input("What is your name?")
print("Hi", name, ". It is nice to meet you!")
Lesson 4: Custom Spiral Maker
import turtle
color = turtle.textinput("Color", "What color do you want your spiral to be? Please give well known colors: ")
color = color.lower()
lines = input("How big do you want your spiral to be(how many lines) Give a number: ")
lines = int(lines)
window = turtle.Screen()
window.title("Spiral Maker")
window.setup(200, 200)
window.bgcolor(color)
t = turtle.Turtle()
t.speed(0)
t.width(width)
t.pencolor("white")
for x in range(lines):
t.forward(x + 3)
t.left(360/4 + 5)
turtle.mainloop()
Lesson 5: Crazy Greeter
print("Hello, I am a program.")
name = input("What is your name?")
for x in range(len(name)):
print("Hello", name)
Lesson 6: Quiz Program
score = 0
q1 = input("What is the capital of the US?").lower()
if(q1 == "washington dc"):
score += 1
print("you are correct!")
else:
print("sorry, that is incorrect")
q2 = input("What is the capital of France?").lower()
if(q2 == "paris"):
score += 1
print("you are correct!")
else:
print("sorry, that is incorrect")
q3 = input("What is the capital of Spain?").lower()
if(q3 == "madrid"):
score += 1
print("you are correct!")
else:
print("sorry, that is incorrect")
print("Good job! Your score is ", score)
Lesson 7: Debug a Program
nums = [1, 2, 3, 4, 5]
sum = 0
#Because sum was inside the for loop, it reset every time the program entered the loop. To fix this, you need to move the variable declaration outside of the loop.
for i in nums: #there was a missing colon after nums
sum = sum + i
if(sum == 3):
print("hello") #missing parenthesis and quotes around hello
print(sum)
Lesson 8: Fortune Cookie Program
import random
fortunes = ["Good things come to those who wait.",
"Patience is a virtue.",
"The early bird gets the worm.",
"A wise man once said, everything in its own time and place.",
"Fortune cookies rarely share fortunes."]
answer = input('Would you like to hear a fortune?(yes or q) ')
while(answer != 'q'):
print(random.choice(fortunes))
answer = input('Would you like to hear another fortune?(yes or q) ')
Lesson 9: Random Color Picker
Easy solution:
import random
colors = ['red', 'yellow', 'blue', 'green', 'purple']
color = random.choice(colors)
print(color)
One-line solution:
import random
print(random.choice(['red', 'yellow', 'blue', 'green', 'purple']))