SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Decision Structures And Boolean Logic
Exercise:
Programming Exercises
Question:5 | ISBN:9780132576376 | Edition: 2

Question

The colors red, blue, and yellow are known as the primary colors because they cannot be made by mixing other colors. When you mix two primary colors, you get a secondary color, as shown here:

When you mix red and blue, you get purple.

When you mix red and yellow, you get orange.

When you mix blue and yellow, you get green.

 

Design a program that prompts the user to enter the names of two primary colors to mix.If the user enters anything other than “red,” “blue,” or “yellow,” the program should display an error message. Otherwise, the program should display the name of the secondary color that results.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

CODE:

P_1 = input("Enter a name of 1st primary color to mix:")      #Asking the user to enter 1ts primary color.
P_2 = input("Enter a name of 2nd primary color to mix:")      #Asking the user to enter 1ts primary color.
Primary_colors = ["red","yellow","blue"]                      #Assiging the primary colors.
if P_1 in Primary_colors and P_2 in Primary_colors: #Checking if user given colors matches primary colors.
    #if yes..
    if P_1=="red" and P_2=="yellow":               #Check the input colors are red and yellow.
        print(" The name of the secondary color is :'Orange'")   # if yes display the respective color.
    if P_1=="yellow" and P_2=="blue":               #Check the input colors are blue and yellow.
        print(" The name of the secondary color is :'Green'")    # if yes display the respective color.
    if P_1=="red" and P_2=="blue":                  #Check the input colors are red and blue.
        print(" The name of the secondary color is :'Purple'")   # if yes display the respective color.
else:                        #if the input colors does not match primary color.
    print("Error....!")          #display an error messaage .

 

OUTPUT:

Enter a name of 1st primary color to mix:red
Enter a name of 2nd primary color to mix:blue
 The name of the secondary color is :'Purple'


Enter a name of 1st primary color to mix:black
Enter a name of 2nd primary color to mix:red
Error....!

 

0 0

Discussions

Post the discussion to improve the above solution.