Friday, February 11, 2022

Thinkful - Logic Drills: Traffic light Python Codewars

 

You're writing code to control your town's traffic lights. You need a function to handle each change from green, to yellow, to red, and then to green again.

Complete the function that takes a string as an argument representing the current state of the light and returns a string representing the state the light should change to.

For example, update_light('green') should return 'yellow'.


def update_light(current):
    order = ['green','yellow','red']
    if current == 'green':
        return 'yellow'
    if current == 'yellow':
        return 'red'
    if current == 'red':
        return 'green'

def update_light(current):
    light_order = {'red':'green', 'yellow':'red', 'green':'yellow'}    
    return light_order[current]

print(update_light('green'))

Thinkful - Logic Drills: Traffic light Solutions | Codewars



Related Posts


No comments :

Post a Comment