jeudi 18 juin 2020

Python: how to return the actual value of a randomly chosen list item instead of its index

I'm trying to create a color matching game that will generate 144 color blocks using random rgb values. I created a list to store my sets of rgb values and I am trying to choose one specific value for the user to match, but every time I try to print the chosen rgb value, it only comes up with one number instead of the three. Here's my code for the RandomColors class:

import random

class RandomColors:
    def __init__(self):
        self.__value = self.get_colors()

    @property
    def value(self):
        return self.__value

    def value(self, value):
        self.__value = value

    def get_colors(self):
        r = random.randrange(0,255)
        g = random.randrange(0,255)
        b = random.randrange(0,255)
        print(r,g,b)

and here's my code for the main class:

from rand_colors import RandomColors
import random

def main():
    randColors = RandomColors()
    colorList = []
    for i in range (1,144):
        randColors.get_colors()
        colorList.append(i)
    num = random.choice(colorList)
    target = colorList[num]
    print("Match: " + str(target))

if __name__ == "__main__":
    main()

I think my issue is in the target = colorList[num] part but I don't know where to go from here. Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire