Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
89 views
in Python by (120 points)
Write a function that takes an integer n and a list c which is a set of characters as parameters. This function should generate and return random words of length n from the characters in list c. If the list is empty, the term "no" will be generated.

Please log in or register to answer this question.

1 Answer

0 votes
by (24.8k points)
import random

def generate_random_word(n, c):
    if not c:
        return "no"
    
    random_word = ''.join(random.choice(c) for _ in range(n))
    return random_word

# Example usage
n = 5
character_list = ['a', 'b', 'c', 'd', 'e']
random_word = generate_random_word(n, character_list)
print("Generated Word:", random_word)

In this example, if n is 5 and character_list contains the characters ['a', 'b', 'c', 'd', 'e'], the function will generate a random word of length 5 using these characters. If character_list is empty, it will return "no".

Related questions

0 votes
1 answer
0 votes
1 answer
asked Apr 4, 2023 in Python by kvdevika (123k points)
0 votes
1 answer
0 votes
1 answer
asked Mar 25, 2023 in Python by kvdevika (123k points)
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...