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"))
#๐ Python program convert english to pig latin
21 messages ยท Page 1 of 1 (latest)
@timber hound
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.
Closes after a period of inactivity, or when you send !close.
i have completed the first 2 requirements i dont have idea where to place if block for next condition .. am i on right track??
- python strings has a method called
x.startswith(str), where it returns true ifxstarts withstr, and false otherwise. (you can use this instead of your.lower()) - My understanding of rule 3 is that it will overwrite rule 2 when appliable, so Iwould put this check before your rule 2 check.
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?
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"):
oh didnt knew startswith accept iterable too...!
!str.startswith
!d str.startswith
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.
this seem better i change it now
it can't acceptlist, but it can with tuple
in my testing
so that's why the tuple() was there
yeah ok ๐
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.