Issue
I am working on a Discord bot in python, and I need a way to generate a random hex color code, I looked it up online and I found this:
import random
color = "%06x" % random.randint(0, 0xFFFFFF)
Is it possible to do the same thing using an f-string?
Solution
import random
color = f"{"%06x" % random.randint(0, 0xFFFFFF)}"
Is this what you want?
However, I don't think this is a good way to create a hex code. Personally, I would do it like this:
import random
random_number = random.randint(0,16777215)
hex_number = str(hex(random_number))
hex_number ='#'+ hex_number[2:]
But that's just a preferance!
Answered By - The Pilot Dude
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.