试着在我的印刷品周围加上引文

2024-10-01 02:28:48 发布

您现在位置:Python中文网/ 问答频道 /正文

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 

我想在每一节的第一行和第三行都有引语。 例如“the end”“ethay endyay”

谢谢!你知道吗


Tags: thetoinforfilewordslinesprint
3条回答

只需在相应的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

只需加上引号:

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

或使用格式:

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

试着这样做:

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

相关问题 更多 >