2013-02-25 55 views
-1
file = open('english.txt','r') 
vowels = ('a', 'e', 'i', 'o', 'u') 

for text in file: 
    translated= [] 
    lines = text.split() 
    print(' '.join(lines)) 
    print("Translated to pig latin becomes:") 
    for words in lines: 
     if words[0] in vowels: 
      words = words + 'yay' 
     else: 
      while words[0] not in vowels: 
       words = words[1:] + words[0] 
      words = words + 'ay' 
     translated.append(words) 
    words = ' '.join(translated) 
    print(words, '\n') 

我得到的結果是:試圖把在報價在我的打印結果

this is a statement 
Translated to pig latin becomes: 
isthay isyay ayay atementstay 

its going through python 
Translated to pig latin becomes: 
itsyay oinggay oughthray onpythay 

and this is the result 
Translated to pig latin becomes: 
andyay isthay isyay ethay esultray 

the end 
Translated to pig latin becomes: 
ethay endyay 

我想在該節的每一個1號線和3號線有報價是他們。 例如「end」「ethay endyay」

謝謝!

回答

0

你可以添加你的報價是這樣的:

print('"', words, '"', '\n') 
+0

這將使各地'words'空間。 – 2013-02-25 00:08:08

+0

哇,它比我做得更容易。謝謝! – user2105660 2013-02-25 00:11:28

2

只需添加引號:

print('"' + words + '"\n') 

或者使用格式:

print('"{}"\n'.format(words)) 
1

試試這樣說:

print('"{}"\n'.format(words)) 
0
def quoted(s): 
    return '"%s"' % s 

print(quoted(words), '\n') 
1

只是圍繞在各自print()語句加上引號的對象:

file = open('english.txt','r') 
vowels = ('a', 'e', 'i', 'o', 'u') 

for text in file: 
    translated= [] 
    lines = text.split() 
    print('"'+' '.join(lines)+'"') # Here 
    print("Translated to pig latin becomes:") 
    for words in lines: 
     if words[0] in vowels: 
      words = words + 'yay' 
     else: 
      while words[0] not in vowels: 
       words = words[1:] + words[0] 
      words = words + 'ay' 
     translated.append(words) 
    words = ' '.join(translated) 
    print('"'+words+'"', '\n') # And here