2014-02-22 3 views
0

내 코드 :파이썬 문자열과 Piglatin

def isVowel(character): 
    vowels = 'aeiouAEIOU' 
    return character in vowels 

def toPigLatin(String): 
    index = 0 
    stringLength = len(String) 
    consonants = '' 

    if isVowel(String[index]): 
     return String + '-ay' 
    else: 
     consonants += String[index] 
     index += 1 
     while index < stringLength: 
      if isVowel(String[index]): 
       return String[index:stringLength] + '-' +consonants + 'ay' 
      else: 
       consonants += String[index] 
       index += 1 
     return 'This word does contain any vowels.' 

def printAsPigLatin(file): 
    return toPigLatin(file) 

이 프로그램은 piglatin로 변환하는 파일 이름을 요청해야합니다. 파일에는 한 줄에 한 단어 씩 들어 있습니다. 내 printAsPigLatin 함수를 사용하여 사용자에게 파일 이름을 묻는 메시지를 돼지 머리글로 변환하도록 요청할 수 있습니까?

+0

CodeAcademy에서 이와 비슷한 문제가 많이 발생합니다. –

답변

1
# grab input file name from user 
read = raw_input("Input filename? ") 

# grab output file name from user 
write = raw_input("Output filename? ") 

# open the files, assign objects to variables 
readfile = open(read, "r") 
writefile = open(write, "w") 

# read each line from input file into list "lines" 
lines = readfile.readlines() 

# for each line in lines, translate the line and write it to the output file 
for line in lines: 
    line = toPigLatin(line.strip("\n")) 
    writefile.write(line + "\n") 

# we're done, close our files! 
readfile.close() 
writefile.close() 
print "Done!" 
관련 문제