You can download this code by clicking the button below.
This code is now available for download.
This function generates a random sentence using a list of words. The length of the sentence is between 1 and max_sentence_length.
Technology Stack : Python built-in libraries (random, fire)
Code Type : Python Function
Code Difficulty : Intermediate
import random
import fire
def random_sentence(words, max_sentence_length=20):
"""Generate a random sentence using a list of words."""
sentence = []
for _ in range(random.randint(1, max_sentence_length)):
sentence.append(random.choice(words))
return ' '.join(sentence)
if __name__ == '__main__':
fire.Fire(random_sentence)