pyglatin without changing punctuation marks - python

Hi i made a pyglatin code:
pyg = 'ay'
original = input("Enter a word:")
if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
new_word = word + first + pyg
new_word = new_word[1:len(new_word)]
print(new_word)
else:
print("empty")
and it works perfectly but i need s.th else.
for example if user entered
"rm !"
then the output should be:
"mray !"
i dont punctuation marks like ! , . # to change.
can anyone help?

Related

Pig Latin Python Translator

I'm making a pig latin translator in python. I'd like to follow the rules of pig latin which state that it is not the first letter, but rather the first sound that moves to the end. As such, a word like "tree" becomes "eetray", and not "reetay". As such, I'd like to make the program detect whether a word has a digraph at the beginning. Here's my code so far:
import twoletterdigraphs
import threeletterdigraphs
pyg = 'ay'
original = input('Enter a word: ')
if len(original) > 0 and original.isalpha():
word = original.lower()
if word[0] + word[1] + word[2] in threeletterdigraphs.threeLetter:
first = word[0] + word[1] + word[2]
new_word = word + first + pyg
new_word = new_word[3:len(new_word)]
print(new_word)
elif word[0] + word[1] in twoletterdigraphs.twoLetter:
first = word[0]+word[1]
new_word = word + first + pyg
new_word = new_word[2:len(new_word)]
print(new_word)
else:
first = word[0]
new_word = word + first + pyg
new_word = new_word[1:len(new_word)]
print(new_word)
else:
print('empty')
twoletterdigraphs and threeletterdigraphs are python files containing nothing but a tuple with a list of digraphs mapped to a variable twoLetter or threeLetter. Here they are:
twoLetter = ["sh" "tr" "ch" "bl" "br" "cl" "cr" "dr" "fl" "fr" "gl" "gr" "pl" "pr" "sc" "sk"
"sl" "sm" "sn" "sp" "st" "sw" "th" "tr" "tw" "wh" "wr"]
and
threeLetter = ["sch" "scr" "shr" "sph" "spl" "spr" "squ" "str" "thr"]
When I run my code, It will only move the first letter to the end, regardless of what word I use. How can this be fixed?

How can I create a code that translates a string sentence to Pyglatin? [duplicate]

This question already has answers here:
python pig latin converter
(2 answers)
Closed 6 years ago.
I have a python code to translate a one worded string to pyglatin and is as follows:
pyg = 'ay'
original = raw_input('Enter a word:')
if len(original)>0 and original.isalpha():
word = original.lower()
first = word[0]
rest = word[1:]
new_word = rest+first+pyg
print new_word
However, I'm stumped on how to translate an entire sentence to Pyglatin. The problem I'm working on has these following conditions: for words that begin with consonants, all initial consonants are moved to the end of the word and 'ay' is appended. For words that begin with a vowel, the initial vowel remains, but 'way' is added to the end of the word.
As an example, the string 'How are you today?' would be 'owhay areway uoyay odaytay?'
Read in a sentence. Break it into individual words (split method). Translate each word to Pig Latin. Concatenate the translations.
Does that get you moving?
Try this. Use split and put it into an empty string.
original = raw_input("Enter Sentence: ")
conversion = ""
for word in original.split():
if len(word)>0 and word.isalpha():
word = word.lower()
first = word[0]
rest = word[1:]
pyg = "ay"
pygword = rest+first+pyg
conversion += pygword + " "
print conversion
Here is my try, but if you are not doing it yourself at least try to understand it. (You are free to ask of course)
This has basic ability to deal with special characters like the "?"
def pygword(word):
vowels = 'aeiou'
if word[0] in vowels:
return word + 'way'
else:
while word[0] not in vowels:
word = word[1:]+word[0]
return word + "ay"
def pygsentence(sentence):
final = ''
for word in sentence.lower().split(): #split the sentence
#words should only end in symols in correct grammar
if word[-1] in '.,;:!?':
symbol = word[-1]
word = word[:-1]
else:
symbol = ''
if word.isalpha(): #check if word is alphanumerically
final += pygword(word)+symbol+' '
else:
return "There is a strange thing in one of your words."
return final[:-1] #remove last unecessary space
There may be faster, more robust, simpler, better understandable ways to do this, but this how I would start.
Test yields me:
In[1]: pygsentence("How are you today? I am fine, thank you very much good sir!")
Out[1]: 'owhay areway ouyay odaytay? iway amway inefay, ankthay ouyay eryvay uchmay oodgay irsay!'
Your code does not obey the vowel/consonant rule, so I did my own converter for single words.
Just realized that it won't be able to deal with apastrophes in the middle of words (we don't really have theese in german ;) ) so there is a little task left for you.
edit: I did not know in which order you wanted the consonants apended, since that became not clear from your example. So i made an alternative pygword function:
def pygword2(word):
vowels = 'aeiou'
if word[0] in vowels:
return word + 'way'
else:
startcons = ''
while word[0] not in vowels:
startcons = word[0] +startcons
word = word[1:]
word = word+startcons
return word + "ay"
See the differnece:
In[48]: pygword("street")
Out[48]: 'eetstray'
In[49]: pygword2("street")
Out[49]: 'eetrtsay'

i need some assistance with my python pyglatin code

ok so the code is
pyg = 'ay'
print "To translate type A SINGLE word or name!"
original = raw_input("Type word Here:")
while len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
new_word = word[1:] + first + pyg
print "Translating 1 moment..."
print "Translated view below!"
print new_word
print "Made by: Tobias Balentine"
raw_input ('pause')
if raw_input ("Do you want to start over?").lower().[0] != 'y': break
so my question is how do i restart to the start of the code without exiting the program when i put (if raw_input ("Do you want to start over?").lower().[0] != 'y': break) it just shows the translation again but i want it to go to the start of the code so you can type a different word to translate how would i go about doing this?
P.S. i am new to python
Copy this statement:
original = raw_input("Type word Here:")
inside your while statement. Your final code should be like this:
pyg = 'ay'
print "To translate type A SINGLE word or name!"
original = raw_input("Type word Here:")
while len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
new_word = word[1:] + first + pyg
print "Translating 1 moment..."
print "Translated view below!"
print new_word
print "Made by: Tobias Balentine"
raw_input ('pause')
if raw_input ("Do you want to start over?").lower().[0] != 'y':
break
original = raw_input("Type word Here:")
As others have indicated, if you want to continually prompt the user for new words, you need to have that prompt be inside of the loop. You could do something like this, where you prompt the user in the loop, and then, if the input is valid, translate the word. If it's not, tell the user so. Then, in either case, you ask the user if she wants to start over, and break the loop if she doesn't say yes (or more accurately, if she types anything that doesn't start with a "Y" or "y").
pyg = 'ay'
print "To translate type A SINGLE word or name!"
while True:
original = raw_input("Type word Here:")
# Validate the input here. If it's valid,
# do the translation.
if original and original.isalpha():
word = original.lower()
first = word[0]
new_word = word[1:] + first + pyg
print "Translating 1 moment..."
print "Translated view below!"
print new_word
else:
# Tell the user if his input is invalid.
print "Your input was stupid. No translation 4 u."
# Ask the user if he wants to start over
if raw_input ("Do you want to start over?").lower()[0] != 'y':
break
# Give em something to remember you by
print "Made by: Tobias Balentine"

My program works but Codeacademy does not acknowledge

Here is my code for the pig latin translator. It works both on Code academy and in linux terminal.
pyg = 'ay'
new_word = pyg
original = raw_input('Enter a word: ')
if len(original) > 0 and original.isalpha():
original.lower()
word = original
first = original[0]
if first == 'a' or first =='e' or first == 'i' or first =='o' or first == 'u':
print 'vowel'
elif first != 'a' or first !='e' or first !='o' or first !='i' or first !='u':
print word.lower()[1:] + first +new_word
else:
print 'empty'
Code academy gives following result;
Oops, try again! Your word started with a consonant, but “ay' was printed instead of “ogday”. Make sure the correct value #is stored in “new_word”.
"ay" is not printed but "ogday' is printed.
Does anyone know how to fix this? I cannot continue with Codeacademy as without solving this.
You can do something like this for example. You are in the right track just use what you have learned in the Code academy up to this task.
consonants = "bcdfghjklmnpqrstvxz"
original = raw_input('Enter a word: ')
if len(original) > 0 and original.isalpha():
if original.lower()[0] in 'aeiou':
print original.lower() + 'ay'
else:
keep_first_consonants = ''
count = 0
for letter in original.lower():
if letter in consonants:
keep_first_consonants = keep_first_consonants + letter
count += 1
else:
break
total_characters = len(original.lower())
print original.lower()[count:total_characters] + keep_first_consonants + 'ay'
else:
print 'Accept only letters'
The codeacademy lesson checker seems to check the variable new_word when you hit run
So you just need to use new_word for both your print varibles
This code works:
pyg = 'ay'
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
if first == "a" or first == "e" or first == "i" or first == "o" or first == "u":
new_word = original + pyg
print new_word
else:
newer_word = word[1:]
new_word = newer_word + first + pyg
print new_word

Python EOF error on Mac OSX

So I am a beginner in coding and I started learning Python a while back! Recently I was writing my own code. It's a translator project from english to pyg latin (Just a made up language adding 'ay' to the word. Now this is my code:
original = raw_input('Name any english word?')
if len(original) > 0 and original.isalpha(): #Testing if variable has characters and not numbers
word = original.lower()
first = word[0]
if first == 'a' or first == 'e' or first == 'i' or first == 'o' or first == 'u':
new_word = word + pyg #Word translated to pyg latin
print new_word
else:
new_word = word[1:] + word[0] + pyg
print new_word
else:
print "empty" #No word active
Problem is I keep getting a EOF error in my 3rd line? What is the problem and what is an EOF error?
The code needs a bit more than some indent fixes, but I'll forgive. This works for me on OSx and PC.
original = raw_input('Name any english word?\n')
PYG = 'ay'
VOWELS = 'aeiou'
if len(original) > 0 and original.isalpha(): #Testing if variable has characters and not numbers
word = original.lower()
first = word[0]
if first in VOWELS:
new_word = ''.join([word, PYG]) #Word translated to pyg latin
print new_word
else:
new_word = ''.join([word[1:], word[0], PYG])
print new_word
else:
print 'Empty'
Using in and ''.join() is easier and more pythonic. '\n' just produces a newline before the input query.
As for your strange error... I expect your Mac was just throwing up something for one of the various problems with the code. EOF means "End of File." raw_input can throw the error but none of the calls you made should produce it normally. Alternatively, it could be because you are unintentionally using an older version of python that comes with your Mac and cannot accommodate some of those calls in the same way.
Anyway, not important. The code should work now.

Categories