本帖最后由 zhi_huo 于 2022-9-6 12:44 编辑
书本:[python编程快速上手......(第2版)] [6.8小程序:Pig Latin]
问题:1、根据下列代码,作者的意思是只检查第一位字母吧?我的理解是:第一位字母不是元音字母的话,就把他移动到最后面并加上‘ay’。是元音字母的话,就直接加‘yay’。所以不太理解元音字母组不加上‘y’时,为什么‘m’没有放到最后面。而元音字母组加上‘y’后,‘m’被放到了最后面
[Python] 纯文本查看 复制代码 # Separate the consonants at the start of this word:
prefixConsonants = ''
while len(word) > 0 and not word[0] in VOWELS:
prefixConsonants += word[0]
word = word[1:]
# Add the Pig Latin ending to the word:
if prefixConsonants != '':
word += prefixConsonants + 'ay'
else:
word += 'yay'
2、为什么加了下列代码,不太理解。下面的代码我的理解是:提取最开头的非字母。但是第一个位置一般不会有非字母吧?
[Python] 纯文本查看 复制代码 prefixNonLetters = ''
while len(word) > 0 and not word[0].isalpha():
prefixNonLetters += word[0]
word = word[1:]
不加“y”的情况下(如下列代码):
[Python] 纯文本查看 复制代码 # English to Pig Latin
# Pig Latin是一种傻乎乎的、可伪造的语言,它会改变英语单词。如果单词以元音开头,就在单词末尾添加yay。
# 如果单词以辅音或辅音簇(例如ch或gr)开头,那么该辅音或辅音簇会移至单词的末尾,然后加上ay。
print('Enter the English message to translate into Pig Latin: ')
message = input()
VOWELS = ('a', 'e', 'i', 'o', 'u')
pigLatin = []
for word in message.split():
# 字符串'My name is AL SWEIGART and I am4,000 years old.'
# 会导致split()方法返回['My','name','is','AL','SWEIGART','and','I','am','4,000','years','old.']。
# Separate the non-letters at the start of this word:
# 我们需要删除每个单词开头和结尾的所有非字母字符,使得像'old.' 这样的字符串转换为'oldyay.',而不是'old.yay'。
# 我们将这些非字母字符保存到名为prefixNonLetters的变量中。
prefixNonLetters = ''
while len(word) > 0 and not word[0].isalpha():
prefixNonLetters += word[0]
word = word[1:]
if len(word) == 0:
pigLatin.append(prefixNonLetters)
continue
# Separate the non-letters at the end of this word:
suffixNonLetters = ''
while not word[-1].isalpha():
suffixNonLetters += word[-1]
word = word[:-1]
# Remember if the word was in uppercase or title case.
# 确保程序能够记住该单词是全大写还是首字母大写,以便能够在将单词翻译成Pig Latin后将其恢复
wasUpper = word.isupper()
wasTitle = word.istitle()
word = word.lower() # Make the word lowercase for translation.
# Separate the consonants at the start of this word:
prefixConsonants = ''
while len(word) > 0 and not word[0] in VOWELS:
prefixConsonants += word[0]
word = word[1:]
# Add the Pig Latin ending to the word:
if prefixConsonants != '':
word += prefixConsonants + 'ay'
else:
word += 'yay'
# Set the word back to uppercase or title case:
if wasUpper:
word = word.upper()
if wasTitle:
word = word.title()
# Add the non-letters back to the start or end of the word.
#在for循环的末尾,我们将该单词及其最初带有的任何非字母前缀或后缀附加到pigLatin列表中
pigLatin.append(prefixNonLetters + word + suffixNonLetters)
print(' '.join(pigLatin))
输出为:
[PowerShell] 纯文本查看 复制代码 Enter the English message to translate into Pig Latin:
My name is AL SWEIGART and I am 4,000 years old.
Myay amenay isyay ALYAY EIGARTSWAY andyay Iyay amyay 4,000 earsyay oldyay.
而书本上的源代码:(元音字母组里有“y”)
[Python] 纯文本查看 复制代码 # English to Pig Latin
print('Enter the English message to translate into Pig Latin:')
message = input()
VOWELS = ('a', 'e', 'i', 'o', 'u', 'y')
pigLatin = [] # A list of the words in Pig Latin.
for word in message.split():
# Separate the non-letters at the start of this word:
prefixNonLetters = ''
while len(word) > 0 and not word[0].isalpha():
prefixNonLetters += word[0]
word = word[1:]
if len(word) == 0:
pigLatin.append(prefixNonLetters)
continue
# Separate the non-letters at the end of this word:
suffixNonLetters = ''
while not word[-1].isalpha():
suffixNonLetters += word[-1]
word = word[:-1]
# Remember if the word was in uppercase or title case.
wasUpper = word.isupper()
wasTitle = word.istitle()
word = word.lower() # Make the word lowercase for translation.
# Separate the consonants at the start of this word:
prefixConsonants = ''
while len(word) > 0 and not word[0] in VOWELS:
prefixConsonants += word[0]
word = word[1:]
# Add the Pig Latin ending to the word:
if prefixConsonants != '':
word += prefixConsonants + 'ay'
else:
word += 'yay'
# Set the word back to uppercase or title case:
if wasUpper:
word = word.upper()
if wasTitle:
word = word.title()
# Add the non-letters back to the start or end of the word.
pigLatin.append(prefixNonLetters + word + suffixNonLetters)
# Join all the words back together into a single string:
print(' '.join(pigLatin))
输出为:
[PowerShell] 纯文本查看 复制代码 Enter the English message to translate into Pig Latin:
My name is AL SWEIGART and I am 4,000 years old.
Ymay amenay isyay ALYAY EIGARTSWAY andyay Iyay amyay 4,000 yearsyay oldyay. |