Stuck with loops in python - only returning first value

后端 未结 5 1235
情话喂你
情话喂你 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:46

    Functions end as soon as a return is reached. You'll need to return once at the end instead of inside the loop:

    def func1(x):
      # The string to work on
      new_str = ""
    
      for (a,b) in enumerate (x):
        # Add to the new string instead of returning immediately
        if a%2 == 0:
          new_str += b.upper()
        else:
          new_str += b.lower()
    
      # Then return the complete string
      return new_str
    

提交回复
热议问题