avatar Bite 86. Create a RGB-to-Hex converter

Designer Mary wants to convert her CSS from statements like background-color: rgb(128, 128, 0); to: background-color: #808000;.

Don't worry, you don't have to rewrite CSS, just complete the rgb_to_hex helper for her that takes a tuple of RGB (3 ints) and converts it to the corresponding hex value.

For example for Maroon you would call it like rgb_to_hex((128, 0, 0)) and it would return the hex #800000.

Make sure you check that each r, g and b int is within the valid range of 0 and 255 (both included), if not raise a ValueError, Explicit is better than implicit (Zen).

Check the TESTS tab for more examples, there we check your code for 16 colors and 3 excepions. Have fun!

Login and get coding
go back Intermediate level
Bitecoin 3X

Will you be Pythonista #260 to crack this Bite?
Resolution time: ~31 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 2.75 on a 1-10 difficulty scale.
» You can do it! 😌

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help