【发布时间】:2020-01-28 15:38:34
【问题描述】:
我需要编写一个 Python 程序,请求一个单词(小写字母)作为输入,并将该单词翻译成 Pig Latin。 • 将单词翻译成 Pig Latin 的规则如下: a) 如果单词以 元音 开头,则在单词末尾添加方式。例如,else 变成 elseway。 b) 如果单词以一组辅音开头,将它们移到单词的末尾并添加ay。比如chip变成ipchay。
我目前的编码:
word = input("Enter word to translate: ")
#if the 1st letter of a word is "aeiou", add "way" to the end of the word
if word[0] == "a" or "e" or "i" or "o" or "u":
print(word + "way")
elif word[0] and word[1] == "b" or "c" or "d" or "f" or "g" or "h" or "j" or "k" or "l" or "m" or "n" or "p" or "q" or "r" or "s" or "t" or "v" or "x" or "y" or "z":
print(word + "ay")
看来我目前的编码有一些问题,因为它只显示单词,在单词的末尾添加“方式”,而不管单词的第一个字母是元音还是辅音。另外,如果它是一组辅音,我不确定如何将单词的第一个字母移动到单词的末尾,并且如果单词中的第一个字母和第二个字母形成辅音,我不知道如何处理“zh”、“ch”等
这个 Python 程序的预期结果: 输入要翻译的单词:否则 Pig Latin 中的单词是 elseway。 输入要翻译的单词:chip Pig Latin 中的单词是 ipchay。
【问题讨论】:
标签: python python-3.x logical-operators