171k views
23 votes
2.9.8 stop light codehs

I cant figure out how to do this stop light function on codehs help
(this is the assignment)
Write a program that prompts the user for a color: red, yellow, or green (the colors of a stoplight). Then using if statements and else statements, print the user a message describing what they should do. For example if the color was red, you could say something like, “Red light: you should stop.”

As a bonus, see if you can use “else if” in this problem. Check out “else if” in the “Docs” tab.

You may assume the user will only input “red”, “yellow”, or “green”. You do not have to worry about other colors, or mixed capitalization such as “blue” or “ReD”, but you can make your program handle these cases if you wish!

User Packoman
by
3.3k points

2 Answers

8 votes

Final answer:

To solve this problem, you can use if statements and else statements in Python to create a program that prompts the user for a color and prints a message based on their input.

Step-by-step explanation:

To solve this problem, you can use if statements and else statements in Python to create a program that prompts the user for a color and prints a message based on their input. Here's an example code:

color = input('Enter a color: ')

if color.lower() == 'red':
print('Red light: You should stop.')
elif color.lower() == 'yellow':
print('Yellow light: Proceed with caution.')
elif color.lower() == 'green':
print('Green light: You can go.')

This code prompts the user for a color, converts the input to lowercase using the lower() method to handle mixed capitalization, and then uses if statements and else if statements to determine what message to print based on the input.

User JRL
by
4.1k points
6 votes

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.

User Armentage
by
2.8k points