jeudi 7 avril 2022

How to return a multi-line string representing a complete poem from function [duplicate]

One was of my task conditions is:

Your script should include a function makePoem() that returns a multi-line string representing a complete poem. The main section of the code should simply print makePoem() to display a single poem.

Nouns = ["fossil", "horse", "aardvark", "judge", "chef", "mango", "extrovert", "gorilla"]
Adverbs = ["curiously", "extravagantly", "tantalizingly", "furiously", "sensuously"]
Adjectives = ["furry", "balding", "incredulous", "fragrant", "exuberant", "glistening"]
Verbs = ["kicks", "jingles", "bounces", "slurps", "meows", "explodes", "curdles"]
Prepositions = ["against", "after", "into", "beneath", "upon", "for", "in", "like", "over", 
"within"]
def makePoem():
    noun1 = random.choice(Nouns)
    noun2 = random.choice(Nouns)
    noun3 = random.choice(Nouns)
    while True:
        if noun1 == noun2:
            noun1 = random.choice(Nouns)
        elif noun2 == noun3:
            noun2 = random.choice(Nouns)
        elif noun1 == noun3:
            noun3 = random.choice(Nouns)
        else:
            break
    adjective1 = random.choice(Adjectives)
    adjective2 = random.choice(Adjectives)
    adjective3 = random.choice(Adjectives)
    while True:
        if adjective1 == adjective2:
            adjective1 = random.choice(Adjectives)
        elif adjective2 == noun3:
            adjective2 = random.choice(Adjectives)
        elif adjective1 == adjective3:
            adjective3 = random.choice(Adjectives)
        else:
            break
    verb1 = random.choice(Verbs)
    verb2 = random.choice(Verbs)
    verb3 = random.choice(Verbs)
    while True:
        if verb1 == verb2:
            verb1 = random.choice(Verbs)
        elif verb2 == verb3:
            verb2 = random.choice(Verbs)
        elif verb1 == verb3:
            verb3 = random.choice(Verbs)
        else:
            break
    adverb1 = random.choice(Adverbs)

    preposition1 = random.choice(Prepositions)
    preposition2 = random.choice(Prepositions)
    while True:
        if preposition1 == preposition2:
            preposition1 = random.choice(Prepositions)
        else:
            break
    vowels = ['a', 'e', 'i', 'o', 'u', 'y']
    if adjective1[0] in vowels:
        art = 'An'
    else:
        art = 'A'
    print('{0} {1} {2}\n\n{0} {1} {2} {3} {4} the {5} {6}\n{7}, the {2} {8}\nthe {6} {9} {10} 
a {11} {12} '
    .format(art, adjective1, noun1, verb1, preposition1, adjective2, noun2, adverb1, verb2, 
verb3, preposition2,adjective3,
    noun3))

makePoem()

Everything works well but I need to return my poem from my function and display it just by writing makePoem() (That's already happens but I don't return it from the function)




Aucun commentaire:

Enregistrer un commentaire