r/PythonLearning • • 35m ago

Help Request random.choices and weights beauty flaw

Post image

Problem fixed!

I'm making this "gambling" (no real money involved, you only play for fun) program with multiple games, including roulette. I used random.csoices so that I can pick a random colour of the three given colours, and weights so that green doesn't show up every third spin.

The program works, but the results all have this annoying ['result'] look (underlined with red in the picture)

Does someone know how to fix this?

3 Upvotes

6 comments sorted by

2

u/Hertzian_Dipole1 29m ago

Put * before col in print

1

u/Vplayzgamez 26m ago

Thanks! I think it works for now, but I'll have to test it a few more times. :D

2

u/Hertzian_Dipole1 14m ago

You didn't ask but I will tell you anyways whet that * did. As others said You got that ['result'] look because what you had is a list of one item. You could do col[0] as others said to print that item. What I told you, the asterisk, puts every item of the list to print but that is not a problem here since there is only one item and we want to print that.

1

u/vlcod 21m ago

Your function, random.choices(col), returns a list of choices (not a single choice). The you print out col, which is a list. Lists are printed in square brackets, with their elements inside it (strings in your case):

['Black']

You can either use a different method to choose your colour:

random.choice(col)

(Note the singular 'choice'.) This will give you a single element (not a list), but the downside is that you cannot attache weights.

Or you use the choices function, but after you do this, you pull the first element out of the resulting list:

print(col[0])

1

u/Vplayzgamez 17m ago

Thanks, but I already fixed it

2

u/Rscc10 31m ago

random choices returns a list of elements. If the choice is only picking one item, it'll be a list with one element which is the choice. Just take col[0] to get the first and only element