2016-04-01 9 views
0
def isAlpha(c): 
return (ord(c) >= 65 and ord(c) <= 95) or \ 
(ord(c) >= 97 and ord(c) <= 122) 

# testing first function 

print isAlpha("D") 
print isAlpha("z") 
print isAlpha("!") 





s = "AEIOUaeiou" 
def isVowel(c): 
    return s.find(c) > -1 

# testing second function 

print isVowel("A") 
print isVowel("B") 
print isVowel("c") 
print isVowel(" ") 
print isVowel("a") 

def convPigLatin_word(word): 
    if isVowel(word[0]): 
     word += "way" 
while not isVowel(word[0]): 
    word = word[1:] + word[0] 
    if isVowel(word[0]): 
     word += "ay" 
return word 

# testing third function 
print convPigLatin_word("This") 
print convPigLatin_word("ayyyyyylmao") 

def translate(phrase): 
final = "" 
while phrase.find(" ") != -1: 
    n = phrase.find(" ") 
    final += convPigLatin_word(phrase[0:n]) + " " 
    phrase = phrase[n+1:] 
if phrase.find(" ") == -1: 
    final += convPigLatin_word(phrase) 
return final 

print translate("Hello, this is team Number Juan") #Should be "elloHay, isthay isway eamtay umberNay uanJay" 

文字列をブタのラテン語に変換するコードを作成しようとしました。しかし、私は非英数字の文字に立ち往生した。 whileループはカンマまでしか動作しません。どうすれば解決できますか?私は英数字以外の文字をチェックするためにisAlphaコードをどこに実装するのかわかりません。アドバイスが役に立ちます。Pythonの文字列をブタのラテン語に変換する

答えて

0

.split( '')を使用すると、フレーズの単語を繰り返し処理できます。次に、.isalpha()を使用して特殊文字をテストできます。

pigLatin = lambda word: word[1:]+ word[0]+"ay" 

def testChars(word): 
    text = "" 
    for char in list(word): 
    if char.isalpha(): 
     text += char 
    else: 
     return pigLatin(text)+ char 

def testWords(lis): 
    words = [] 
    lis = lis.split(' ') 
    for word in lis: 
    if not word.isalpha(): 
     words.append(testChars(word)) 
    else: 
     words.append(pigLatin(word)) 
    return (' ').join(words) 

phrase = "I, have, lots of! special> characters;" 
print testWords(phrase) 
関連する問題