samedi 16 mai 2020

How do I break down if statement inside the while loop to two separate funcitons?

The idea is to keep repeat the input unless enter 'm'. I'm trying to pass the latinize_sentence function to input_latin after 'else:'(to be exact)

However, The latinize_sentence function would better not be changed if not necessary, so it may be tested separately later.

Write a program that repeatedly asks the user to enter sentences and prints
out the Bee Latin versions of those sentences. 
The user can enter 'm' to quit the program.


def main():
    """Set up the main function to process the latin format"""
    sentence = input_latin("Enter English sentence: ")
    print_latin(sentence)


def input_latin(words):
    """Make the input repeat over unless encounter 'm'"""
    looping = False
    while not looping:
        sentence = input(words)
        if sentence == 'm':
            looping = True
            return sentence
        else:
            latin = latinize_sentence(sentence)
            return latin


def latinize_sentence(sentence):
    """return the Bee Latin version of the sentence."""
    latin = sentence.lower().split()
    for word_num in range(len(latin)):
        if latin[word_num][0] in "aeiou" or sentence[word_num][0].isalpha() == False:
            latin[word_num] += 'buzz'
        else:
            latin[word_num] = latin[word_num][1:] + \
                latin[word_num][0]
            latin[word_num] += 'uzz'
        latin = ' '.join(latin)
        return latin


def print_latin(sentence):
    """Print out the result"""
    if sentence == 'm':
        print("oodbyeguzz")
    else:
        print("Bee latin = {}".format(sentence))


main()


#Example test:
#Testing main()
Enter English sentence: try this out
Bee latin = rytuzz histuzz outbuzz
Enter English sentence: and that #Repeatly
Bee latin = andbuzz hattuzz
Enter English sentence: m #stop repeating until type 'm'
oodbyeguzz

#Testing latinize_sentence:

english = latinize_sentence("try this out")
print(english)
rytuzz histuzz outbuzz

Aucun commentaire:

Enregistrer un commentaire