Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pig_拉丁翻译公司;for循环_Python_Python 3.x - Fatal编程技术网

Python Pig_拉丁翻译公司;for循环

Python Pig_拉丁翻译公司;for循环,python,python-3.x,Python,Python 3.x,Pig Latin translator基本上是许多项目的入门代码,它涉及到让以辅音开头的单词将第一个字母移动到单词的末尾并添加“ay”,以及只向以元音开头的单词添加“ay”。除了一些地方,我基本上完成了整个项目,一个主要的事情是让程序用for循环完成整个句子,特别是在移动到下一个列表项时 我尝试过做一些简单的事情,比如在for循环的末尾做n+1,并在网上进行了研究(大多数是关于Stackoverflow的) 没有错误消息,但是,计算机运行变量“1”并不是作为变量first_单词的第一个(第零个

Pig Latin translator基本上是许多项目的入门代码,它涉及到让以辅音开头的单词将第一个字母移动到单词的末尾并添加“ay”,以及只向以元音开头的单词添加“ay”。除了一些地方,我基本上完成了整个项目,一个主要的事情是让程序用for循环完成整个句子,特别是在移动到下一个列表项时

我尝试过做一些简单的事情,比如在for循环的末尾做n+1,并在网上进行了研究(大多数是关于Stackoverflow的)

没有错误消息,但是,计算机运行变量“1”并不是作为变量first_单词的第一个(第零个)字母,而是从a-z开始运行。有没有办法解决这个问题?多谢各位

#!/usr/bin/env python

sentence = input("Insert your sentence ")

# don't forget "y" :)
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']

# this empty list will hold our output
output_words = list()

# we'll take the user's input, strip any trailing / leading white space (eg.: new lines), and split it into words, using spaces as separators.
for word in sentence.strip().split():
    first_letter = word[0]
    # check if the first letter (converted to lower case) is in the consonants list
    if first_letter.lower() in consonants:
        # if yes, take the rest of the word, add the lower case first_letter, then add "ay"
        pig_word = word[1:] + first_letter.lower() + "ay"
    # if it's not a consonant, do nothing :(
    else:
        pig_word = word
    # add our word to the output list
    output_words.append(pig_word)

# print the list of output words, joining them together with a space
print(" ".join(output_words))
循环在句子中的每个单词上循环-无需使用
n
计算任何内容。也没有必要在辅音或元音上循环,我们只关心“这个单词是否以辅音开头”——如果是,请按照您的规则修改它

我们将(可能的)修改后的单词存储在输出列表中,完成后,我们打印所有单词,并用空格连接

请注意,这段代码有很多错误-如果用户的输入包含标点符号,会发生什么情况


I opehay hattay elpshay,eyuleochoujay

您的代码运行吗?当我复制粘贴它时,它不会运行。它会运行,但结果真的很奇怪。Danielle,谢谢你的回答,我想问一些关于函数的问题并澄清一些事情。那么,第28行的.append函数和代码末尾的.join函数是什么。@JeyuLeoChou
append()
是一个列表方法-
output\u words
以空列表开始。然后我们将
.append()
每个单词添加到列表中,不管我们是否将其翻译成拉丁语。完成后,我们将
.join()
这些单词中的每一个组合成一个字符串,并使用
“”
空格作为每个单词和
打印()
字符串之间的分隔符。
#!/usr/bin/env python

sentence = input("Insert your sentence ")

# don't forget "y" :)
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']

# this empty list will hold our output
output_words = list()

# we'll take the user's input, strip any trailing / leading white space (eg.: new lines), and split it into words, using spaces as separators.
for word in sentence.strip().split():
    first_letter = word[0]
    # check if the first letter (converted to lower case) is in the consonants list
    if first_letter.lower() in consonants:
        # if yes, take the rest of the word, add the lower case first_letter, then add "ay"
        pig_word = word[1:] + first_letter.lower() + "ay"
    # if it's not a consonant, do nothing :(
    else:
        pig_word = word
    # add our word to the output list
    output_words.append(pig_word)

# print the list of output words, joining them together with a space
print(" ".join(output_words))