random.choice HELP - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: random.choice HELP (/thread-34546.html) |
random.choice HELP - samuelbachorik - Aug-08-2021 Hi iam using random.choice and i want to ask you, how to know what element of array was choosen ? example code I need to store in variable what element from array was choosen. i mean number of element Is it possible ? import random array = ["Hello","Dog","Cat","Ball"] x = random.choice(array) print(x) RE: random.choice HELP - menator01 - Aug-08-2021 Here is one way import random array = ["Hello","Dog","Cat","Ball"] x = random.choice(array) print(f'Text -> {x} index -> {array.index(x)}')
RE: random.choice HELP - samuelbachorik - Aug-08-2021 (Aug-08-2021, 10:58 AM)menator01 Wrote: Here is one way THANK YOU A LOT RE: random.choice HELP - deanhystad - Aug-08-2021 I index = random.randint(0, len(array)-1) x = array[index] RE: random.choice HELP - naughtyCat - Aug-18-2021 import random array = ["Hello","Dog","Cat","Ball"] x = random.choice(range(len(array))) print(x, array[x]) |