Random Sentence Generator with Seed Word

  • Share this:

Code introduction


The code defines a function named random_sentence that accepts two parameters: arg1 (an integer representing the number of words in the sentence) and arg2 (a string with a default value of 'Hello', serving as the seed word for the sentence). The function generates a random sentence where words are randomly selected from a predefined list, and the number of words is determined by arg1.


Technology Stack : Typer

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
from typer import Typer, Argument, Option, echo

def random_sentence(arg1: int, arg2: str = "Hello"):
    """Generate a random sentence based on the number of words and a seed word.

    Args:
        arg1 (int): The number of words in the sentence.
        arg2 (str, optional): The seed word for the sentence. Defaults to "Hello".

    Returns:
        str: A random sentence.
    """
    words = ["apple", "banana", "cherry", "dog", "elephant", "frog", "grape", "ice cream", "juice", "kiwi"]
    sentence = [arg2]
    for _ in range(arg1 - 1):
        sentence.append(words[len(sentence) % len(words)])
    echo(" ".join(sentence))

random_sentence_app = Typer()
random_sentence_app.add_typer_command(random_sentence)

if __name__ == "__main__":
    random_sentence_app.run()                
              
Tags: