语法无效,因为逗号

时间:2018-06-22 11:44:39

标签: python-3.x

我试图做一个计算元音,辅音和其他字符的程序。问题在于返回原因是逗号,我不明白为什么。例如,应该返回“ 2个元音,3个辅音和5个其他元音”。 这是我的程序(对于任何英语错误,我们深表歉意):

    def count1(word):
        vowels = 0
        consonants = 0
        others = 0

        l1 = ['A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u']

        for i in range(len(word)):
           if word[i] in l1:
              vowels = vowels + 1

           elif (word[i] >= 'a' and word[i] <= 'z') or (word[i] >= 
            'A' and word[i] <= 'Z'):
              consonants = consonants + 1

           else :
            others = others+ 1
        return(vowels "vowels," consonants "consonants," outros "others")

1 个答案:

答案 0 :(得分:2)

您应先格式化字符串,然后再返回:

return '{} vowels, {} consonants, {} others'.format(vowels, consonants, others)

或者在Python 3.6+中使用f-string

return f'{vowels} vowels, {consonants} consonants, {others} others'