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
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
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'