Study Guide

2.17.5 Happy/Sad Face CodeHS Answers

2.17.5 Happy/Sad Face CodeHS Answers

The most common answer is:

speed(0)
Draws the initial circle and two eyes
def smileycircle():
penup()
setposition (0,-100)
pendown()
color(“yellow”)
begin_fill()
circle(100)
end_fill()
color(“black”)
penup()
pensize(1)
setposition(-25,25)
for i in range (2):
begin_fill()
circle(5)
end_fill()
penup()
forward(50)
pendown()
smileycircle()
Draws a smile
def smileylove():
penup()
setposition (-50,-25)
left(270)
pendown()
circle (50, 180)
penup()
Draws a frown
def smileyfrown():
penup()
setposition(50,-50)
pendown()
left(90)
circle(50,180)
penup()
The illusion of choice…
happy = input (“Are you happy? (Yes/No):”)
if happy == (“Yes”):
smileylove()
else:
smileyfrown()

To create a program that draws a happy or sad face based on user input, a few adjustments are needed in this code for it to work correctly.

Here is a corrected and optimized version of the code:

import turtle

turtle.speed(0)  # Set the drawing speed to the fastest

# Draws the initial circle and two eyes
def smileycircle():
    turtle.penup()
    turtle.setposition(0, -100)
    turtle.pendown()
    turtle.color("yellow")
    turtle.begin_fill()
    turtle.circle(100)
    turtle.end_fill()
    turtle.color("black")
    turtle.penup()
    turtle.pensize(1)
    turtle.setposition(-35, 35)  # Adjust position for the left eye
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(5)
    turtle.end_fill()
    turtle.penup()
    turtle.setposition(35, 35)  # Adjust position for the right eye
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(5)
    turtle.end_fill()

# Draws a smile
def smileylove():
    turtle.penup()
    turtle.setposition(-50, -25)
    turtle.pendown()
    turtle.setheading(-60)  # Adjust the orientation for the smile
    turtle.circle(50, 120)  # Draw the smile
    turtle.penup()

# Draws a frown
def smileyfrown():
    turtle.penup()
    turtle.setposition(-50, -15)  # Adjust starting position for the frown
    turtle.pendown()
    turtle.setheading(-120)  # Adjust the orientation for the frown
    turtle.circle(50, -120)  # Draw the frown
    turtle.penup()

# Draw the face base and eyes first
smileycircle()

# The illusion of choice...
happy = input("Are you happy? (Yes/No): ")

if happy.lower() == "yes":
    smileylove()  # If user is happy, draw a smile
else:
    smileyfrown()  # If user is not happy, draw a frown

turtle.done()  # Keep the window open until manually closed

This version includes the following adjustments:

  • Importing the turtle module at the beginning.
  • Defining the smileycircle function to draw the base circle and eyes.
  • Adjusting the positions for the eyes to be symmetrical and correctly aligned with the face.
  • Adding turtle.setheading() in the smileylove and smileyfrown functions to ensure the mouth starts drawing from the correct orientation.
  • Using happy.lower() to make the input case-insensitive, ensuring that the program behaves correctly whether the user inputs “yes”, “YES”, “Yes”, etc.

Based on the user’s input, the program will draw a happy face with a smile or a sad face with a frown.