Part of the series

Several example codes

~2 min read • Updated Oct 1, 2025

Program Overview

This Python program reads a single character from the user that represents a color.
Based on the input, it displays the name of the selected color.
To exit the program, the user must enter E or e.


Python Code:


# Loop until user chooses to exit
while True:
    ch = input("Enter a character (R/G/B/Y for color, E to exit): ").strip()

    if ch.lower() == 'e':
        print("Exiting the program...")
        break
    elif ch.lower() == 'r':
        print("You selected Red.")
    elif ch.lower() == 'g':
        print("You selected Green.")
    elif ch.lower() == 'b':
        print("You selected Blue.")
    elif ch.lower() == 'y':
        print("You selected Yellow.")
    else:
        print("Invalid character! Please enter R/G/B/Y or E.")

Sample Output:


Enter a character (R/G/B/Y for color, E to exit): r  
You selected Red.

Enter a character: g  
You selected Green.

Enter a character: e  
Exiting the program...

Step-by-Step Explanation:

- The program runs in a loop until the user enters E or e
- The input character is normalized using .lower() for easier comparison
- Based on the character, the corresponding color name is printed
- If the character is invalid, an error message is shown


Written & researched by Dr. Shahin Siami