Generating a Random Hex Color in Python

前端 未结 15 1776
悲&欢浪女
悲&欢浪女 2020-11-29 01:36

For a Django App, each \"member\" is assigned a color to help identify them. Their color is stored in the database and then printed/copied into the HTML when it is needed. T

15条回答
  •  粉色の甜心
    2020-11-29 02:28

    import random
    
    def get_random_hex:
        random_number = random.randint(0,16777215)
    
        # convert to hexadecimal
        hex_number = str(hex(random_number))
    
        # remove 0x and prepend '#'
        return'#'+ hex_number[2:]
    

提交回复
热议问题