1

I'm trying to do a hangman game with a few twists and I used a random module to make a random choice from a few text files with different topics like animals, food, countries.. that I transferred to different lists and I don't know how to make python choose now a random word from all the lists. What I mean is I want it to choose a random list and from the list a random word I didn't find any solutions to this on the internet yet but maybe I'm just dumb

import random

colors_txt = open('colors.txt', 'r')
color_lst = []
for color in colors_txt:
    color_lst.append(color.replace('\n', ''))

random_color = random.sample(color_lst, 1)

This is the code for example I know there is function random.choice() instead but I just used this one

2
  • So you have a list of lists and you want to choose a random item from a random list in that list? Commented Jan 26, 2021 at 19:43
  • No I have 4 lists with just a lot of words and I want to choose a random word from a random list out of that 4 Commented Jan 26, 2021 at 19:50

2 Answers 2

1

I think this is what you're asking

import random

lists = [[...], [...], ...]

chosenList = random.choice(lists)
output = random.choice(chosenList)
Sign up to request clarification or add additional context in comments.

Comments

1

Try this :

import random

lists = [...] #your lists as a list of lists
#picks a list randomly
randomList = random.choice(lists)
#picks an item in the randomly found list
result = random.choice(randomList)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.