Enums
Completed
Create class Light(Enum) with RED, YELLOW, GREEN, and a function next_light(light) (RED→GREEN→YELLOW→RED). Read a colour name and a number of steps; print each light name as it changes.
Input
Line 1: colour. Line 2: steps.
Output
One name per step.
Example
Input
RED 4
Output
GREEN YELLOW RED GREEN
Editor
Python
Hints
Light["RED"] looks a member up by name; .name gives it back as text.
Solution explanation
Enums give fixed sets of named constants that can't be mistyped as strings.
Show solution
from enum import Enum
class Light(Enum):
RED = 1
YELLOW = 2
GREEN = 3
ORDER = {Light.RED: Light.GREEN, Light.GREEN: Light.YELLOW, Light.YELLOW: Light.RED}
def next_light(light):
return ORDER[light]
light = Light[input()]
for _ in range(int(input())):
light = next_light(light)
print(light.name)