#๐Ÿ”’ Python program convert english to pig latin

21 messages ยท Page 1 of 1 (latest)

timber hound
#
def translate(text):
    sentence = text.split()
    piggysentence = []
    vowels = ["a","e","i","o","u"]

    for word in sentence:
        if word[0].lower() in vowels or word[:2].lower() in ("xr","yt"):  # for words with vowel starting
            piggyword = word + "ay"
            piggysentence.append(piggyword)
        else: # for consonants starting
            
            cons_num = 0  #keep track of consonants
            for letter in word:
                if letter.lower() not in vowels:
                    cons_num += 1
                else:
                    break
            
            print(cons_num,word)
            piggyword = word[cons_num:] + word[:cons_num] + "ay"
            piggysentence.append(piggyword)
    return piggysentence


print(translate("chcair"))



gaunt hawkBOT
#

@timber hound

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

timber hound
timber hound
timid hound
#
  1. python strings has a method called x.startswith(str), where it returns true if x starts with str, and false otherwise. (you can use this instead of your .lower())
  2. My understanding of rule 3 is that it will overwrite rule 2 when appliable, so Iwould put this check before your rule 2 check.
timber hound
# timid hound 1. python strings has a method called `x.startswith(str)`, where it returns true...
def translate(text):
    sentence = text.split()
    piggysentence = []
    vowels = ["a","e","i","o","u"]

    for word in sentence:
        if word[0].lower() in vowels or word[:2].lower() in ("xr","yt"):  # for words with vowel starting
            piggyword = word + "ay"
            piggysentence.append(piggyword)
        else: # for consonants starting
            
            cons_num = 0  #keep track of consonants
            for letter in word:
                letter = letter.lower()
                if letter == "u":
                    index_l = word.index(letter)
                    if word[index_l - 1] == "q":
                        cons_num += 1
                        continue
                if letter not in vowels:
                    cons_num += 1
                else:
                    print("thiss",letter)
                    break
            
            print(cons_num,word)
            piggyword = word[cons_num:] + word[:cons_num] + "ay"
            piggysentence.append(piggyword)
    return piggysentence


print(translate("square"))



``` i hardcoded like this is this ok?
#

oh i could use enumerate instead?

timid hound
#

well my idea is check if the word.startswith(constant + 'qu')

#

where the constant part you figure out

#

and when I say replace.lower() i meant you can write

if word.startswith(tuple(vowels), 'xr', 'yt'):
#instead of 
if word[0].lower() in vowels or word[:2].lower() in ("xr","yt"):
timber hound
#

!str.startswith

#

!d str.startswith

gaunt hawkBOT
#

str.startswith(prefix[, start[, end]])```
Return `True` if string starts with the *prefix*, otherwise return `False`. *prefix* can also be a tuple of prefixes to look for. With optional *start*, test string beginning at that position. With optional *end*, stop comparing string at that position.
timber hound
#

this seem better i change it now

timid hound
#

it can't acceptlist, but it can with tuple

#

in my testing

#

so that's why the tuple() was there

timber hound
#

yeah ok ๐Ÿ‘

gaunt hawkBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.