-3

How do I get python to choose a random item from whatever the person wrote?, what I mean lets say the person wrote (Apple, Banana, Orange) and then python would choose a random one from these 3 fruits?

How do I get it to do that? (the person who is using the app would write the items, I meant python would ask for example name the fruits and the person would write the fruits (not the programmer) and python would choose a random one. I'm thinking of using len but not sure how)

1 Answers1

0

Assuming you have ingested your user input into a list, you can use the random module:

import random

user_inputs = ["Apple", "Banana", "Orange"]

random_fruit = random.choice(user_inputs)

If you need more than one random choice, you can use sample:

 random_fruit_sample = random.sample(user_inputs, 2)
PirateNinjas
  • 1,908
  • 1
  • 16
  • 21
  • wait i meant python would ask for example name the fruits and the person would write the fruits (not the programmer) and python would choose a random one. im thinking of using len but not sure how – Malak Anas May 15 '23 at 11:47