-1

Input: 3

gandhi
mahatma gandhI
Mohndas KaramChand gandhi

Output:

Gandhi 
M. Gandhi 
M. K. Gandhi 

And I wrote the following code:

n =  int(input())


while n > 0  :
  k = raw_input()

  if k.find(" ") != -1:

    final = str(k[0].upper() + '.')


    for i in range(len(k)) :
      if (k[i] == ' ') & (k[i:].find(" ")):
        final += k[i+1].upper() + ". "
      else:
          if(k[i] == ' '):
            final += k[i+1].upper() + k[i+2:].lower()


    n -= 1

    print final[0:len(final)]

 else :
    print  k[0].upper() + k[1:].lower()

I'm getting a runtime error for this. Can someone explain why? ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

ɢʀᴜɴᴛ
  • 32,025
  • 15
  • 116
  • 110
  • What are the run time errors you get? – Rumesh Eranga Hapuarachchi Jul 26 '17 at 09:11
  • Welcome to Stack Overflow! To help people answer your question, you'll need to be more specific about the error. Please [edit] your post to incorporate the exact errors you get from your [mcve] (preferably using copy+paste to avoid transcription errors). – Toby Speight Jul 26 '17 at 09:13

1 Answers1

0

try this code.

str = "gandhi mahatma gandhi Mohndas KaramChand gandhi"
temp = ''
for st in str.split():
    if st == 'gandhi':
        temp = temp + ' ' + st.title()
    else:
        temp = temp + ' ' + st[0].upper() + '.'
print temp

Output as per your expectation - Gandhi M. Gandhi M. K. Gandhi

SumanKalyan
  • 1,681
  • 14
  • 24