这个IF ELSE声明有什么问题

时间:2015-12-10 16:57:03

标签: python arrays if-statement

这段代码有什么问题? 我试图运行这个,但每次我运行这个我得到这个错误:

  

否则如果array [i] ==“d”或array [i] ==“f”或array [i] ==“g”:

     

语法错误:语法无效

我已尝试过所有功能,但无法解决问题。请帮助我,并提前感谢你。

input = "ypyoz"
array =[]
alphabet = [" ","b","c","d", "f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"]
for i in range(len(input)):
    array.append(input[i])

for i in range(len(array)):
    if array[i] in ('a', 'e', 'i', 'o', 'u'):
        array[i] = array[i]
    else:
        if array[i] == "b" or array[i] == "c":
            vowel = "a"
        else if array[i] == "d" or array[i] == "f" or array[i] == "g":
            vowel = "e"
        else if array[i] == "h" or array[i] == "j" or array[i] == "k" or array[i] == "l":
            vowel = "i"
        else if array[i] == "m" or array[i] == "n" or array[i] == "p" or array[i] == "q" or array[i] == "r":
            vowel = "o"
        else if array[i] == "s" or array[i] == "t" or array[i] == "v" or array[i] == "w" or array[i] == "x" or array[i] == "y" or array[i] == "z":
            vowel = "u" 
        consenent = alphabet.index(array[i])


        array[i] = array[i] + vowel + alphabet[consenent + 1]
print ''.join(array)

1 个答案:

答案 0 :(得分:1)

Python没有使用else if。您必须改为使用elif

for i in range(len(array)):
    if array[i] in ('a', 'e', 'i', 'o', 'u'):
        array[i] = array[i]
    else:
        if array[i] == "b" or array[i] == "c":
            vowel = "a"
        elif array[i] == "d" or array[i] == "f" or array[i] == "g":
            vowel = "e"
        elif array[i] == "h" or array[i] == "j" or array[i] == "k" or array[i] == "l":
            vowel = "i"
        elif array[i] == "m" or array[i] == "n" or array[i] == "p" or array[i] == "q" or array[i] == "r":
            vowel = "o"
        elif array[i] == "s" or array[i] == "t" or array[i] == "v" or array[i] == "w" or array[i] == "x" or array[i] == "y" or array[i] == "z":
            vowel = "u" 
        consenent = alphabet.index(array[i])


        array[i] = array[i] + vowel + alphabet[consenent + 1]
print ''.join(array)