Skip to content
Worked Examples · Example 6.3

Q.Display the appropriate message as per the colour of signal at the road crossing.

Ladakh CbseNCERTSubjective· 2mImportance★★★★★est
13% · 5/39 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 →

Map each signal colour to its message with an if–elif–else ladder on the input string, normalised with upper() so the comparison is case-insensitive.

The idea. The traffic signal is a multi-way selection over string values: three known colours, each with its own message, plus a default for anything unrecognised. An if–elif ladder with string equality tests expresses this directly. Because the user may type red, Red or RED, we normalise the input once with upper() instead of testing every spelling.

Program

colour = input("Enter the colour of the signal: ")
colour = colour.upper()          # case-insensitive matching

if colour == "RED":
    print("STOP")
elif colour == "YELLOW":
    print("WAIT")
elif colour == "GREEN":
    print("GO")
else:
    print("Invalid colour. Please enter RED, YELLOW or GREEN.")

Sample runs

Enter the colour of the signal: red
STOP
Enter the colour of the signal: Green
GO
Enter the colour of the signal: blue
Invalid colour. Please enter RED, YELLOW or GREEN.
``` …

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.