I have been trying code an RGB to Hex function in Python when I faced a problem that I wasn't able to figure out how to do.
Here is the function itself:
def rgb(r, g, b):
return ''.join([format("{0:x}".format(x).rjust(2, "0").upper()) if int(x) >= 0 else "00" if int(x) <= 255 else "FF" for x in [r,g,b]])
The important part: if int(x) >= 0 else "00" if int(x) <= 255 else "FF"
What I want to be able to do is to apply a different output if the number is lower than 0 or higher than 255. Only the first if works, the second is ignored. How can we properly do multiple conditions in a list comprehension?