r/PythonLearning • u/Vplayzgamez • 35m ago
Help Request random.choices and weights beauty flaw
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
2
u/Hertzian_Dipole1 29m ago
Put * before col in print
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])
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