The most common answer is:
rating= 8
def red_ex():
pensize(5)
color("red")
left(45)
for i in range(4):
forward(100)
backward(100)
left(90)
def yellow_line():
pensize(5)
color("yellow")
forward(100)
def green_check():
pensize(5)
color("green")
left(45)
for i in range(2):
forward(100)
backward(100)
number = int(input("Enter a number: "))
if number < 5:
red_ex()
elif number == 7:
yellow_line()
else:
green_check()
Turtle graphics allows for drawing shapes and patterns by moving a “turtle” around the screen. The script defines three different functions (red_ex
, yellow_line
, and green_check
) to draw specific shapes in different colors, and then executes one of these functions based on user input. Here’s a breakdown of how the script works:
- Setting a Default Rating: Initially, a variable
rating
is set to 8, but it’s not used anywhere in the provided code. It might be a remnant from an earlier version of the script or intended for future use. - Function Definitions:
red_ex()
: Draws an “X” shape in red. It sets the pen size to 5, changes the color to red, rotates 45 degrees to the left, and then draws lines in a loop to create the “X” shape.yellow_line()
: Draws a single line in yellow. It sets the pen size to 5, changes the color to yellow, and moves forward to draw the line.green_check()
: Draws a checkmark shape in green. It sets the pen size to 5, changes the color to green, rotates 45 degrees to the left, and then draws lines in a loop to create the checkmark.
- User Input: The script asks the user to enter a number via the
input()
function. Based on this input, it decides which shape to draw:- If the number is less than 5, it calls
red_ex()
to draw a red “X”. - If the number is exactly 7, it calls
yellow_line()
to draw a yellow line. - For any other number, it calls
green_check()
to draw a green checkmark.
- If the number is less than 5, it calls
- Execution Flow: The script waits for the user’s input, evaluates the input number, and then executes one of the defined functions to draw the corresponding shape.
This script illustrates conditional logic, function definition and invocation, and the basics of turtle graphics in Python. However, to run this script, you must ensure it’s executed in an environment where the turtle graphics library is supported and properly imported. The pensize()
, color()
, left()
, forward()
, backward()
, and similar functions are part of the turtle graphics API, and they control the drawing turtle’s appearance and movement.
Without the necessary import statement (usually import turtle
at the beginning of the script), the script would raise an error due to undefined functions.