0

I have a Python problem where i need to take input as a string in a function and need to return back a string where every alternate letter is a sequence of small case and Capital case. Ex: String passed to the function: AmsTerdam then the returned string should be AmStErDaM. It can start with any case i.e., small case or capital case.

I am still in learning phase of Python and have come up with the following but somehow when i try to execute, the code hangs. Could anyone please help me in fixing this?

def myfunc(NAME='AmsTerdam'):
    leng=len(NAME)
    ind=1
    newlist=[]
    while ind <= leng:
        if ind%2==0:
            newlist.append(NAME[ind-1].upper())
        else:
            newlist.append(NAME[ind-1].lower())
    str(mylist)   # Can we typecast a list to a string?
    return newlist

OUT=myfunc('Ankitkumarsharma')
print('Output: {}'.format(OUT))

If the typecasting cannot be done, is the following correct?

def myfunc(NAME='AmsTerdam'):
    leng=len(NAME)
    ind=1
    newstr=''
    while ind <= leng:
        if ind%2==0:
            newstr=newstr+NAME[ind-1].upper()
        else:
            newstr=newstr+NAME[ind-1].lower()
    return newstr

OUT=myfunc('AmsTerdam')
print('Output: {}'.format(OUT))
Ankit Vashistha
  • 325
  • 6
  • 17
  • 2
    You never modify the index inside your loop (`ind += 1` inside the loop), I'm not sure what you're trying to typecast though – Sayse May 08 '18 at 12:14
  • 2
    `''.join(mylist)` creates a string from a list – Chris_Rands May 08 '18 at 12:15
  • @Sayse I was thinking of putting every letter converted into upper or lower case into a list and then was trying to typecast that to a string. I am not sure if this is possible. – Ankit Vashistha May 08 '18 at 12:16
  • @Chris_Rands: Wow, thanks this will be helpful. – Ankit Vashistha May 08 '18 at 12:17
  • Please edit your title to actually describe the problem you're having. – glibdud May 08 '18 at 12:17
  • 2
    Possible duplicate of [Capitalise every other letter in a string in Python?](https://stackoverflow.com/questions/17865563/capitalise-every-other-letter-in-a-string-in-python) – Sayse May 08 '18 at 12:18
  • No worries as Sayse says, you also need to decrement `ind` *inside* your loop – Chris_Rands May 08 '18 at 12:18
  • You mean `''.join(a.upper()+b.lower() for a,b in zip(NAME[0::2],NAME[1::2]))` But that only works on strings of `len(NAME) % 2 == 0`. – Dan D. May 08 '18 at 12:22
  • Thank you all for the comments and suggestions. Thanks for highlighting that i had missed the terminate condition for while loop. The second one worked when i placed the ind+=1 which terminates the while loop. – Ankit Vashistha May 08 '18 at 12:29

2 Answers2

1

You have in essence, written a while true loop, without a break condition.

Going by your previous logic we can rewrite your loop and assume ind=1 being always the case, we get:

def myfunc(NAME='AmsTerdam'):
  leng=len(NAME)
  newstr=''
  while 1 <= leng:
      if ind%2==0:
          newstr=newstr+NAME[ind-1].upper()
      else:
          newstr=newstr+NAME[ind-1].lower()
  return newstr

Which means if len(name) > 1, the loop will run forever. Fixing that, we get the following function, which will terminate.

def myfunc(NAME='AmsTerdam'):
  leng=len(NAME)
  newstr=''
  ind=1
  while ind <= leng:
      if ind%2==0:
          newstr=newstr+NAME[ind-1].upper()
      else:
          newstr=newstr+NAME[ind-1].lower()
      ind+=1
  return newstr
John
  • 549
  • 4
  • 12
  • Sweet. I just noticed from this and above comments that i had completely forgot about the terminate condition in loop. This worked. Thanks for the explanation. – Ankit Vashistha May 08 '18 at 12:27
0
def alternat_case(word):
    word2 = []
    for i in range(len(word)):
        if i%2 ==0:
            word2.append(word[i].upper())
        else:
            word2.append(word[i].lower())
    word2 = "".join(word2)    
    return print(word2)

alternat_case("python")
PyThOn
Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40