jeudi 11 mars 2021

for loop in python translator [closed]

I am doing a basic translator on python with these rules:

-when there is a vowel it translates to vowel+f+vowel

-when there is a consonant it remains the same

I have more theoretical than practical doubts. This is the code and it works:

def translate(phrase):
    translation=''
    for letter in phrase:
        if letter in 'AEIOUaeiou':
            translation=translation+letter+'f'+letter
        else:
            letter=translation+letter
    return translation

print(translate(input('Enter a phrase:')))

Why do I need translation at the right of = ? Why if I leave it, the script does not work as required? And also why can't I write a script like this:

def translate(phrase):
    for letter in phrase:
        if letter in 'AEIOUaeiou':
            letter=letter+'f'+letter
        else:
            letter=letter #or maybe I can write just pass
    return letter

print(translate(input('Enter a phrase:')))

Thanks in advance, sorry for the silly question but I am a total beginner

Aucun commentaire:

Enregistrer un commentaire