python中的交替字母

时间:2018-04-26 17:58:09

标签: python

def myfunc(word):
    result = ""
    index = 0
    for letter in word:
        if index % 2 == 0:
            result += letter.lower()
        else:
            result += letter.upper()
    return result
    index +=1

我正在尝试返回一个匹配的字符串,其中每个偶数字母都是大写的,每个奇数字母都是小写的。但是代码没有显示出这个确切的结果,任何解决方案?

11 个答案:

答案 0 :(得分:2)

问题是你只是在循环之后递增index ,而不是每次都通过它。因此,在循环内部,它始终为0.最小的修复是:

def myfunc(word):
    result = ""
    index = 0
    for letter in word:
        if index % 2 == 0:
            result += letter.lower()
        else:
            result += letter.upper()
        index += 1
    return result

但是这种错误很容易制作(有时候不像调试这么容易) - 这正是Python拥有像enumerate这样的好工具的原因,这使得它不可能出错:

def myfunc(word):
    result = ""
    for index, letter in enumerate(word):
        if index % 2 == 0:
            result += letter.lower()
        else:
            result += letter.upper()
    return result

答案 1 :(得分:2)

问题在于您的增量方式。您只需将索引设置为在代码的“ Else”块内递增。它从“ If”块中丢失。这样,一旦您输入“ If”块,您就会被卡在那里。

def myfunc(string):
        result = ""
        index = 0 
        for letter in string:
            if index % 2 == 0:
                result += letter.upper()
                index += 1
            else:
                result += letter.lower()
                index += 1
        return result

答案 2 :(得分:1)

人们including myself已经指出了您的编程错误。以下是使用生成器表达式和ternary conditional operator

解决问题的替代单行解决方案
def myfunc(word):
    return "".join(w.upper() if i%2 else w.lower() for i,w in enumerate(word))

enumerate将为iterable中的每个项返回(index, value)形式的元组。在这种情况下,iterable是字符串word

在迭代的每一步,我们检查索引i是否为奇数。

    对于偶数,
  • i%2将返回0,而if语句将评估为False
  • 同样,对于奇数,它将评估为True

我们分别在当前字符lower()上调用upper()w

最后,我们使用str.join将所有单个字母连接在一起。在这里,我们使用""加入字符,其中是空字符串。

答案 3 :(得分:1)

def myfunc(word):
result = ""
for index, letter in enumerate(word):
    if index % 2 == 0:
        result += letter.lower()
    else:
        result += letter.upper()
return result

这对我有用。 如果您对枚举函数的理解很好,那么理解上面的代码块就会容易得多

答案 4 :(得分:0)

def myfunc(word):
    index = 0
    result = ''

    for letter in word:
        if index % 2 == 0:
            result += letter.lower()
        else:
            result += letter.upper()
        index += 1
    print result

您没有在正确的位置增加索引;)

如果您执行myfunc(word),则会打印hElLo

答案 5 :(得分:0)

def gonna(st) :
      a = []
      Index = 0
      for index, c in enumerate(st) :
             if index ℅ 2 == 0:
                  a.append(c.upper()) 
                  Index = Index + 1
             else:
                  a.append(c.lower()) 
                  Index = Index + 1
        return a

答案 6 :(得分:0)

def myfunc(a):
    result=""
    for x in range(0,len(a)):
        if x%2==0:
            result=result+a[x].upper()
        else:
            result=result+a[x].lower()
    return result

答案 7 :(得分:0)

def myfunc(word):
z=list(word)
x=[]
y=[]
new_list=[]
str=""
for a in z:
    x+=[a]
    if len(x)==2:
        y+=[x]
        x=[]

for i in y:
    odd=i[0].lower()
    even=i[1].upper()
    new_list.append(odd)
    new_list.append(even)

for el in new_list:
    str+=el
return str

答案 8 :(得分:0)

def myfunc(str):
    # Create an empty string to append the values to
    result = ''
    # Iterate through the loop using the enumerate function on the string so that you can use the index and the letter at the same time.
    for index,letter in enumerate(str):
        if index %2 == 0:
            result += letter.lower()
        else:
            result += letter.upper()
            
    # Return the string after all the letters have been appended to the string
    return result

答案 9 :(得分:-1)

def myfunc(word):
    index=0
    result = ''
    for letter in word:
        if index%2==0:
            result=result+letter.upper()
        else:
            result=result+letter.lower()
        index+=1
    return result

答案 10 :(得分:-1)

**

  • 标题

**

def myfunc(word): 结果=“” 对于索引,使用枚举(单词)中的字母: 如果索引%2 == 0: 结果+ = letter.upper() 其他: 结果+ = letter.lower() 返回结果