209k views
4 votes
#this is 34 lines -- you can do it! # #In web development, it is common to represent a color like #this: # # rgb(red_val, green_val, blue_val) # #where red_val, green_val and blue_val would be substituted #with values from 0-255 telling the computer how much to #light up that portion of the pixel. For example: # # - rgb(255, 0, 0) would make a color red. # - rgb(255, 255, 0) would make yellow, because it is equal # parts red and green. # - rgb(0, 0, 0) would make black, the absence of all color. # - rgb(255, 255, 255) would make white, the presence of all # colors equally. # #Don't let the function-like syntax here confuse you: here, #these are just strings. The string "rgb(0, 255, 0)" #represents the color green. # #Write a function called "find_color" that accepts a single #argument expected to be a string as just described. Your #function should return a simplified version of the color #that is represented according to the following rules: # # If there is more red than any other color, return "red". # If there is more green than any other color, return "green". # If there is more blue than any other color, return "blue". # If there are equal parts red and green, return "yellow". # If there are equal parts red and blue, return "purple". # If there are equal parts green and blue, return "teal". # If there are equal parts red, green, and blue, return "gray". # (even though this might be white or black). #Write your function here!

1 Answer

2 votes

Answer:

Following are the code to this question:

def find_color(color):#definig a method find_color that accepts color parameter

color = str(color).replace('rgb(', '').replace(')', '')#definig color variable that convert parameter value in string and remove brackets

r, g, b = int(color.split(', ')[0]), int(color.split(', ')[1]), int(color.split(', ')[2])#defining r,g,b variable that splits and convert number value into integer

if r == g == b:#defining if block to check if r, g, b value is equal

return "gray"#return value gray

elif r > g and r > b:#defining elif block that checks value of r is greater then g and b

return "red"#return value red

elif b > g and b > r:#defining elif block that checks value of b is greater then g and r

return "blue"#return value blue

elif g > r and g > b:#defining elif block that checks value of g is greater then r and b

return "green"#return value green

elif r == g:#defining elif block that checks r is equal to g

return "yellow"#return value yellow

elif g == b:#defining elif block that checks g is equal to b

return "teal"#return value teal

elif r == b:#defining elif block that checks r is equal to b

return "purple"#return value purple

print(find_color("rgb(125, 50, 75)"))#using print method to call find_color method that accepts value and print its return value

print(find_color("rgb(125, 17, 125)"))#using print method to call find_color method that accepts value and print its return value

print(find_color("rgb(217, 217, 217)"))#using print method to call find_color method that accepts value and print its return value

Output:

red

purple

gray

Step-by-step explanation:

In the above method "find_color" is declared that uses the color variable as the parameter, inside the method a color variable is declared that convert method parameter value into the string and remove its brackets, and three variable "r,g, and b" is defined. In this variable first, it splits parameter value and after that, it converts its value into an integer and uses the multiple conditional statements to return its calculated value.

  • if block checks the r, g, and b value it all value is equal it will return a string value, that is "gray" otherwise it will go to elif block.
  • In this block, it checks the value of r is greater then g, and b if it is true it will return a string value, that is "red" otherwise it will go to another elif block.
  • In this block, it checks the value of b is greater then g, and r if it is true it will return a string value, that is "blue" otherwise it will go to another elif block.
  • In this block, it checks the value of g is greater then r and b if it is true it will return a string value, that is "green" otherwise it will go to another elif block.
  • In the other block, it checks r is equal to g or g is equal to b or r is equal to b, it will return the value, that is "yellow" or "teal" or "purple".
User Flakshack
by
4.7k points