0

I'm having trouble with some code:

I'm trying to code so that a sentence's letters alternate in case using an index number.

E.G:

input:hello im an idiot
output: ToIdI nA mI oLlEh
cs95
  • 379,657
  • 97
  • 704
  • 746

1 Answers1

2

As simple as :

>>> string='hello im an idiot'
>>> out=''
>>> caps=True   #flag to see if upper case 

>>> for s in string[::-1]:        #string[::-1] reverses it
        if s==' ':                #when the char is a whitespace, directly add it to the resultant output
          out+=' '
          continue
        if caps:                 #should be uppercase
          out+=s.upper() 
          caps = False 
        else:                    #should be lowercase
          out+=s.lower() 
          caps = True 

>>> out
=> 'ToIdI nA mI oLlEh'
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60