Traffic signal

Problem Statement

Company ABC is building an automatic car. Its camera system can detect the active color of the traffic signal light. The company needs a program to perform the operation: move, break, or ready, based on the active color of the traffic signal light.

Write a program to return the command for a specific traffic signal light that is ON based on the following mapping.

  • Return go command if the active signal light is green.
  • Return stop command if the active signal light is red.
  • Return slow down command if the active signal light is yellow.
  • Return empty string if the active signal light is none of the above mentioned colors.

Input

The first line of input contains a string value representing the active color of traffic signal light.

Output

Print the command string: stop, go, slow down, or an empty string, based on the input.

As is

The boilerplate code is already there to read the input from user into variable color, in the main() function. Also it calls the getCommand() function with color passed as argument, and prints the returned value. Do not edit this code.

To do ✍

getCommand() has one parameter: color. Write code inside the function body to return the appropriate command as a string, based on the input color value.

Example 1

Test input

red

Test output

stop

Example 2

Test input

green

Test output

go

Useful References

Python Program

# Complete the following function
# Return the command string
def getCommand(color):








# Boilerplate code - Do not edit the following
def main():
    color = input()
    print(getCommand(color))

if __name__ == "__main__":
    main()

Testcase 1

red
stop

Testcase 2

green
go

Testcase 3

yellow
slow down

Testcase 4

black
# Complete the following function
# Return the command string
def getCommand(color):
    if color == 'red':
        return 'stop'
    elif color == 'green':
        return 'go'
    elif color == 'yellow':
        return 'slow down'
    else:
        return ''

# Boilerplate code - Do not edit the following
def main():
    color = input()
    print(getCommand(color))

if __name__ == "__main__":
    main()
show answer
main.py
⏵︎ Run Tests
Reset
Download
×
Code copied to clipboard successfully 👍