2
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

I am trying to return a matching string where every even letter is uppercase and every odd letter is lowercase. But the code doesn't show this exact result, any solution?

Bob
  • 1,779
  • 3
  • 22
  • 35
Bijay Gurung
  • 31
  • 1
  • 1
  • 7

12 Answers12

6

The problem is that you're only incrementing index after the loop, rather than each time through it. So, inside the loop, it's always 0. The smallest fix is:

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

But this kind of mistake is very easy to make (and sometimes not as easy as this to debug)—which is exactly why Python has nice tools like enumerate, that make it impossible to get wrong:

def myfunc(word):
    result = ""
    for index, letter in enumerate(word):
        if index % 2 == 0:
            result += letter.lower()
        else:
            result += letter.upper()
    return result
abarnert
  • 354,177
  • 51
  • 601
  • 671
3

People, including myself, have already pointed out your programming error. Here is an alternative one-liner solution to your problem using a generator expression and a ternary conditional operator:

def myfunc(word):
    return "".join(w.upper() if i%2 else w.lower() for i,w in enumerate(word))

enumerate will return a tuple of the form (index, value) for each item in the iterable. In this case, the iterable is the string word.

At each step in the iteration, we check to see if the index i is odd.

  • i%2 will return 0 for even numbers and the if statement will evaluate to False.
  • Likewise, it will evaluate to True for odd numbers.

Respectively, we call lower() and upper() on the current character w.

Finally we use str.join to concatenate all the individual letters back together. Here we join the characters using an "" with is the empty string.

pault
  • 41,343
  • 15
  • 107
  • 149
2

The problem was with how you were incrementing. You only set up your index to increment inside the "Else" block of your code. It was missing from the "If" block. As such as soon as you entered the "If" block you would be stuck there.

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
Gino
  • 1,043
  • 16
  • 27
1
def myfunc(word):
result = ""
for index, letter in enumerate(word):
    if index % 2 == 0:
        result += letter.lower()
    else:
        result += letter.upper()
return result

this worked for me. Also it is much easier to understand the above block of code if you understand the enumerate function well

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

You weren't increment your index in the correct spot ;)

If you execute myfunc(word) it will print hElLo

Harrison
  • 5,095
  • 7
  • 40
  • 60
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
richyen
  • 8,114
  • 4
  • 13
  • 28
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
narayana
  • 1
  • 3
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
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
Jay
  • 5
  • 1
  • 4
0

More Simpler , which is made using all the basic conecpts of Python

def myfunc(string):
    new_string=""
    for items in range(len(string)): # help us to to know about the index
        if items % 2 == 0: 
            new_string = new_string + string[items].upper()
        else:
            new_string = new_string + string[items].lower()
    return new_string
result=myfunc("Draco")
print(result)
Dharman
  • 30,962
  • 25
  • 85
  • 135
-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
alan.elkin
  • 954
  • 1
  • 10
  • 19
  • 2
    While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Mickael B. May 07 '20 at 03:04
-1

**

  • Heading

**

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