samedi 27 mars 2021

Python - Can I color strings that are inside a list?

Fist of all I would like to clarify that at the time of writing this I'm a complete newbie to Python and programming in general, I've only picked up Python yesterday.

I'm learning the language while working on a personal proyect.

My code takes numbers from 1 to 5, runs them through a random.choices Method and weighs them, then returns a random list that would look like this:

[2,1,4,5,2] Let's call this list xs

Each of those numbers has a list of strings asociated to them, let's call them "xsn"

So 1 has xs1 = ['1stringA','1stringB',...,'1stringZ'], same with 2,3,4 and 5

I then run xs through this code

while True:
    for i in range(5):
        if xs[i] == 1:
            xs[i] = (random.choice(xs1))
        if xs[i] == 2:
            xs[i] = (random.choice(xs2))
        if xs[i] == 3:
            xs[i] = (random.choice(xs3))    [Tips on how to improve this code are also accepted and apreciated]
        if xs[i] == 4:
            xs[i] = (random.choice(xs4))
        if xs[i] == 5:
            xs[i] = (random.choice(xs5))
    print(xs)

This will give me a modified xs list that, instead of numbers from 1 to 5, has strings. So if the last list was

[2,1,4,5,2]

The new xs list would be

['2stringA','1stringA','4stringA','5stringA','2stringB']

What I want is for each number to also have it's own asociated color, so '2stringA' and '2stringB' would be Green, '1stringA' would be White and so on.

Any ideas on how to make this happen? Sorry for writing such a long post, as I get better I hope I can sumarise my questions in a more condensed manner.




Aucun commentaire:

Enregistrer un commentaire