So, I want to write this program where I define a class, and the method within it takes a list of colours as an argument. The colours "red", "Red, "green" and "Green" should be replaced with "black", "Black", "white" and "White" (see the dictionary "self.colour_replacement" in the code-text).
If only one of the two colors red/green regardsless of capital letter, is in the list, the program should only return the list without changing it.
Example_1:
print(c.make_readable(['Green', 'Green', 'pink', 'green', 'yellow', 'green',
'green']))
should return:
['Green', 'Green', 'pink', 'green', 'yellow', 'green', 'green']
Example_2:
print(c.make_readable(['green', 'Green']))
should return:
['green', 'Green']
I think the problem has to do with my "or" and "and" statements in the line:
if ('red' and 'green') in colours or ('Red' and 'green') in colours or
('red' and 'Green') in colours or ('Red' and 'Green') in colours:
but I am not entirely sure.
class ColourChanger:
def __init__(self):
"""A dictionary that shows the
replacement colours."""
self.colour_replacement = {'Green' : 'White', 'red': 'black',
'green': 'white', 'Red' : 'Black'}
def make_readable(self, colours):
result = []
if ('red' and 'green') in colours or ('Red' and 'green') in colours or
('red' and 'Green') in colours or ('Red' and 'Green') in colours:
for col in colours:
if col in self.colour_replacement:
result.append(self.colour_replacement[col]) """Appends the
"result"-list with the replacement color instead of the color (col)."""
else:
result.append(col)
else:
return colours
return result
c = ColourChanger()
print(c.make_readable(['green', 'Green']))
print(c.make_readable(['Red', 'red']))
print(c.make_readable(['Red', 'Red', 'pink', 'red', 'yellow', 'red', 'red',
'Green']))
print(c.make_readable(['Green', 'Green', 'pink', 'green', 'yellow', 'green',
'green']))
Expected output:
['green', 'Green']
['Red', 'red']
['Black', 'Black', 'pink', 'black', 'yellow', 'black', 'black', 'White']
['Green', 'Green', 'pink', 'green', 'yellow', 'green', 'green']
Actual Output:
['white', 'White']
['Red', 'red']
['Black', 'Black', 'pink', 'black', 'yellow', 'black', 'black', 'White']
['White', 'White', 'pink', 'white', 'yellow', 'white', 'white']