Stuck with loops in python - only returning first value

后端 未结 5 1243
情话喂你
情话喂你 2020-12-02 01:16

I am a beginner in Python trying to make a function that will capitalize all of the values with an even index, and make lowercase all of the values with an odd index.

5条回答
  •  感情败类
    2020-12-02 01:35

    You are returning in the first iteration of the loop.

    Append the alphabets in a list and return concatinated. Also add 1 to a while checking condition if you want even index to capitalize as the index starts from 0. Use below example:

        def func1(x):
            result = []
            for (a,b) in enumerate (x):
                if (a+1)%2 == 0:
                    result.append(b.upper())
                else:
                    result.append(b.lower())
            return "".join(result)
    
    
        print func1('Testing Testing')
    

    Output:

        tEsTiNg tEsTiNg
    

提交回复
热议问题