- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我从编程开始,自学 Python 3.4.3。我有一个关于 pig 拉丁语翻译器的问题(似乎是每个人最喜欢编写的程序,并提出问题)。
我的代码似乎工作正常,除非我写了一个只有 y 作为其中唯一元音的单词。
def pig_latin(word):
print(word)#test
if word[0] in 'bcdfghjklmnpqrstvwxyz':
i = 0
print(word[i]) #test
while word[i] in 'bcdfghjklmnpqrstvwxyz':
i += 1
word = word[i:] + word[:i] + 'ay'
elif word[0] in 'aeiou':
word += 'way'
return word
def main():
sentence = input('Enter a phrase to convert to pig latin: ')
word = ''
pig_sentence = ''
i = 0
for i in range(len(sentence)):
if sentence[i] != ' ':
word += sentence[i]
else:
pig_sentence += pig_latin(word) + ' '
word = ''
pig_sentence += pig_latin(word)
print(pig_sentence)
if __name__ == '__main__':
main()
我收到的错误与这行代码有关:
while word[i] in 'bcdfghjklmnpqrstvwxyz':
错误是:IndexError: string index out of range
在玩了很多东西之后,我有点难过。任何帮助将不胜感激。
最佳答案
您的问题是单词中没有元音。如果没有元音字母,则以下 while
循环永远不会停止,您会得到一个 IndexError:
while word[i] in 'bcdfghjklmnpqrstvwxyz':
i += 1
word = word[i:] + word[:i] + 'ay'
要解决这个问题,请检查 i 是否小于单词的长度:
while i < len(word) and word[i] in 'bcdfghjklmnpqrstvwxyz':
i += 1
word = word[i:] + word[:i] + 'ay'
现在有了这个实现,第一个字符不会移动到单词的末尾(即 gym 变成 gymay),所以为此写一个条件:
while i < len(word) and word[i] in 'bcdfghjklmnpqrstvwxyz':
i += 1
if i == len(word):
i = 1
word = word[i:] + word[:i] + 'ay'
关于python - 无法在 pig 拉丁语翻译器中将 y 转换为元音词(即健身房),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33005290/
我最近发现需要某种类型的“翻译”类型,可以将特定文本字段或区域翻译成其他语言。 我希望用户何时在 **** 中写入文本html 控件和空格后的文本应转换为本地语言即印地语、阿拉伯语 芬兰语 我不确定是
我正在做一个初学者练习,创建一个 rövarspråket 翻译器。 Write a function translate() that will translate a text into “röv
class seh_exception : public std::exception { public: seh_exception(UINT se_code, PEXCEPTION_P
如果以下代码我没有得到status 200和responseText。但此 URL 有效:http://api.microsofttranslator.com/V2/Http.svc/GetLangu
我想为 Illuminate\Translate\Translator 实现一些额外的功能. 所以,我在 ~/vendor 中创建我的文件夹目录,放在那里My/Traslator类,将实现 Symfo
我经常看到(并重用)没有适当输出的第 3 方源代码。是否有任何工具(代码翻译器)可以将 println 输出转换为合适的日志框架代码 private void processCreateTrainin
我正在制作一个 Pig Latin 翻译器,将用户的输入翻译成 Pig Latin。我已经弄清楚了单词何时以元音开头以及何时不将第一个字母放在中间。然而,当涉及到辅音簇(单词中第一个元音之前的字符组)
这就是我想要的: var elite = document.getElementById("elite"), leet = document.getElementById("leet"), a
因此,我拥有将英语翻译成 Pig Latin 的程序的全部功能。我有大约 80% 的反向翻译功能。 我有两个问题: 首先,当从 Pig Latin 翻译回英语时,它会在它遇到的第一个超过 7 个字符的
VOWELS = ('a', 'e', 'i', 'o', 'u') def pigLatin(word): first_letter = word[0] if first_lette
我有一组用 jquery 编写的表单验证规则 validator插入。由于我必须在服务器端重复相同的验证,所以我认为不必在 PHP 中重写我的规则会很好。如果规则很简单 field=>rulename
gobuffalo i18n 翻译器可在操作中访问,但如何在我的模型上访问它?它需要 buffalo.Context 作为 T *i18n.Translator 中的参数。 提前致谢! 最佳答案 这是
我一直在努力翻译我的应用程序,我从我的数据库中提取翻译并将它们添加到目录中。如果我用 $translator->trans('TEST_TRANSLATION') 回显翻译,一切都在 Symfony
这是我第一次使用 i18next,我不知道如何使它工作。 (似乎 documentation 对我来说是不完整的) 这是我使用 i18next 的 HTML 代码
是否有任何简单的方法可以将 Prolog/ASP 代码转换为 CLIPS? 类似于此,但使用 CLIPS 而不是 Prover9: https://github.com/potassco/anthem
我正在寻找一个免费的 Java 翻译 API,我发现 microsoft-translator-java-api ,它非常容易使用,我喜欢这一点,但是当我从英语翻译成阿拉伯语时,如下所示: publi
如何修改此代码以翻译页面上唯一一个 ID 为 #lyrics 的容器? function googleTranslateElementInit() { new google
我需要将 PHP 源代码编译为 LLVM 位码。我试过 Raven PHP ( http://code.roadsend.com/rphp ),它能够做我想做的事,但该项目似乎已经死了。所以我想检查一
五年多来,我一直在使用此代码在 Excel VBA 宏中将用户输入的英语文本转换为法语或德语。那是在 Microsoft Azure Marketplace 上,由于我的使用量很少,所以它是免费的。
在尝试 Azure 文本翻译 API 时,我偶然发现了应用 ID 或订阅 ID 的一些问题。文档中的应用程序 ID 似乎令人困惑,它不是可以在 Azure 门户中找到的订阅 ID,而是资源 > key
我是一名优秀的程序员,十分优秀!