Ask coding questions

← Back to all posts
reading random code (python)
rosetea73 (0)

i have a project that i've only just started with. i've done the majority myself and am pretty proud of it, but i wanted to know if there's a way to get python to make a random choice from a list, and then recognise what the choice was so i can use it with an 'if' statement?
an example to make it clearer? i want it to choose randomly from A, B or C, and recognise which it chose so I can use it again in an 'if' statement?
there might not be a way, i'm not entirely sure - if there isn't would anyone be able to suggest a way around it? thank you!

  • river
Answered by 19ecal (230) [earned 5 cycles]
View Answer
19ecal (230)

Use random.choice()

import random
_list=[1, 3, 5, "hi there"]
if random_choice=="hi there":
elif random_choice==1:
  #do something else