Final answer:
To complete the CodeHS stop light function, you write a program that prompts for a traffic light color, processes that input with if, elif, and else statements, and prints out instructions such as 'Stop' for red, 'Slow down' for yellow, and 'Go' for green.
Step-by-step explanation:
To solve the 2.9.8 stop light assignment on CodeHS, you'll want to write a program that takes user input and then uses conditional statements to display the correct action based on the traffic light color provided. Here is a simple example using Python:
# Prompt the user for input
light_color = input("Enter a color (red, yellow, green): ").lower()
# Use if, elif, else statements to determine the action
if light_color == 'red':
print("Red light: Stop.")
elif light_color == 'yellow':
print("Yellow light: Slow down, prepare to stop.")
elif light_color == 'green':
print("Green light: Go.")
else:
print("Invalid color entered.")
This program first prompts the user for a color input and converts the string to lowercase to handle mixed capitalization. Then it uses an if statement to check if the input is 'red', elif statements to check for 'yellow' and 'green', and an else statement to handle any unexpected inputs. This aligns with the expected behavior of traffic lights, where a red light means stop, a yellow light means slow down, and a green light means go.