In: Computer Science
The colours red, blue, and yellow are known as the primary colours because they cannot be made by mixing other colours.
When you mix two primary colours, you get one of following secondary colour:
-Mix red and blue: purple
-Mix red and yellow: orange
-Mix blue and yellow: green
Write a function colour_mix that takes two strings as parameters as two primary colours and returns the secondary colour.
If the parameters are anything other than "red", "blue", or "yellow", the function
should return the string "Error". Save the function in a PyDev library module named functions.py.
Write a main program named t05.py that tests the function by asking the user to enter the names of two primary colours to mix and displays the mixed
colour.
A sample run:
Enter a first colour: red
Enter a second
colour: blue
The mixed colour is purple.
Or
Enter a first colour: red
Enter a second colour: brown
The mixed colour is Error
.
code:
functions.py:
#defining the function colour mix
def colour_mix(col1,col2):
#if color is red and blue
if ((col1=="red" and col2=="blue") or(col1=="blue" and
col2=="red")):
res="purple"
#if color is red and yellow
elif ((col1=="red" and col2=="yellow")
or(col1=="yellow" and col2=="red")):
res="orange"
#if color is blue and yellow
elif ((col1=="blue" and col2=="yellow")
or(col1=="yellow" and col2=="blue")):
res="green"
#if invalid input
else:
res="error"
#returning res
return res
t05.py
#importing the module
import functions
#taking inputs from user
color1=input("Enter Color 1: ")
color2=input("Enter color 2 :" )
#storing the return value in result from calling the function
result=functions.colour_mix(color1,color2)
#printing result
print(result)
Output:
Code Screenshot:
Code Snippet:
functions.py
#defining the function colour mix
def colour_mix(col1,col2):
#if color is red and blue
if ((col1=="red" and col2=="blue") or(col1=="blue" and col2=="red")):
res="purple"
#if color is red and yellow
elif ((col1=="red" and col2=="yellow") or(col1=="yellow" and col2=="red")):
res="orange"
#if color is blue and yellow
elif ((col1=="blue" and col2=="yellow") or(col1=="yellow" and col2=="blue")):
res="green"
#if invalid input
else:
res="error"
#returning res
return res
t05.py
#importing the module
import functions
#taking inputs from user
color1=input("Enter Color 1: ")
color2=input("Enter color 2 :" )
#storing the return value in result from calling the function
result=functions.colour_mix(color1,color2)
#printing result
print(result)