Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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 TypeError:只能将str(而不是“int”连接到str(我认为不应该发生这种情况)_Python - Fatal编程技术网

Python TypeError:只能将str(而不是“int”连接到str(我认为不应该发生这种情况)

Python TypeError:只能将str(而不是“int”连接到str(我认为不应该发生这种情况),python,Python,我认为这将是一个很酷的想法,使一个自定义语言的翻译,所以我尝试了一个。然而,我对python还相当陌生,我不明白为什么它需要字符串而不是整数。我想做的是,如果你输入一个单词,比如“bin”,它会进入每个单词的下一个辅音/元音,因此“bin”最后会变成“cop”,因为“b”后面的下一个辅音是“c”,“I”后面的下一个元音是“o”,而“n”后面的下一个辅音是“p” consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n',

我认为这将是一个很酷的想法,使一个自定义语言的翻译,所以我尝试了一个。然而,我对python还相当陌生,我不明白为什么它需要字符串而不是整数。我想做的是,如果你输入一个单词,比如“bin”,它会进入每个单词的下一个辅音/元音,因此“bin”最后会变成“cop”,因为“b”后面的下一个辅音是“c”,“I”后面的下一个元音是“o”,而“n”后面的下一个辅音是“p”

consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
vowels = ['a', 'e', 'i', 'o', 'u']
translated_word = ''

word_to_translate = input('Enter in the word to translate! ')

for letter in range(len(word_to_translate)):
    new_letter = word_to_translate[letter - 1]
    if new_letter in consonants:
        l = (consonants[:new_letter + 1])
        translated_word = translated_word + str(l)
    elif new_letter in vowels:
        l = (vowels[:new_letter + 1])
        translated_word = translated_word + str(l)

print(translated_word)

new_letter+1
正在尝试将
1
添加到字符串中…我可以做些什么来修复此问题?重新思考您在那里尝试做什么。
'a'+1
应该做什么?@deceze它应该让像“bin”这样的词变成“cop”。这是因为“b”之后的下一个辅音是“c”,“i”之后的下一个元音是“o”,而“n”之后的下一个辅音是“p”。a+1应该选择列表中a后面的下一个字符。
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
vowels = ['a', 'e', 'i', 'o', 'u']
translated_word = ''

word_to_translate = input('Enter in the word to translate! ')

for i in word_to_translate:
    if i in consonants:
        ind = consonants.index(i)
        translated_word += consonants[ind+1] 
    elif i in vowels:
        ind = vowels.index(i)
        translated_word += vowels[ind+1]
print (translated_word)