Skip to content
Programs · Program 7-13

Q.Program 7-13: Write a program that simulates a traffic light. The program should consist of the following:
  1. A user defined function trafficLight( ) that accepts input from the user, displays an error message if the user enters anything other than RED, YELLOW, and GREEN. Function light() is called and following is displayed depending upon return value from light().

a) “STOP, your life is precious” if the value returned by light() is 0.
b) “Please WAIT, till the light is Green “ if the value returned by light() is 1
c) “GO! Thank you for being patient” if the value returned by light() is 2.
2. A user defined function light() that accepts a string as input and returns 0 when the input is RED, 1 when the input is YELLOW and 2 when the input is GREEN. The input should be passed as an argument.
3. Display “ SPEED THRILLS BUT KILLS” after the function trafficLight( ) is executed.
Puducherry CbseNCERTSubjective· 5mImportance★★★★★est
38% · 15/40 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Program 7-13 splits the traffic-light simulation across two user-defined functions — light() converts the colour to a return code (0/1/2), trafficLight() validates input and maps the code to the road message — with the safety slogan printed after the function call.

The idea. This is functional decomposition with a return value as the interface between two functions: light() knows only the colour→code rule; trafficLight() knows only validation and presentation. Either can change without touching the other.

# Program 7-13: traffic light simulation

def light(colour):
    """Return 0 for RED, 1 for YELLOW, 2 for GREEN."""
    if colour == "RED":
        return 0
    elif colour == "YELLOW":
        return 1
    elif colour == "GREEN":
        return 2

def trafficLight():
    signal = input("Enter the colour of the traffic light (RED/YELLOW/GREEN): ")
    if signal not in ("RED", "YELLOW", "GREEN"):
        print("Please enter a valid colour of the traffic light")
    else:
        value = light(signal)          # signal passed as an argument
        if value == 0:
            print("STOP, your life is precious")
        elif value == 1:
            print("Please WAIT, till the light is Green")
        else:
            print("GO! Thank you for being patient")

trafficLight()
print("SPEED THRILLS BUT KILLS")

Key lines

  • signal not in ("RED", "YELLOW", "GREEN") — the validation demanded in part 1; anything else (including lower-case "red") is rejected.
  • value = light(signal) — the colour is passed as an argument, and the returned code drives the message ladder.
  • The slogan is a top-level statement after the call, so it prints in every case — valid or invalid input.

Expected output — three sample runs:

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.