-1

So I've written a code which does that but can't seem to convert this thisDoesAThing to this_does_a_thing but rather giving an output like this thisDoesA_thing So help me please This is my code:

import string
test = input("Enter your camelCasing")
loop = 1
x = string.ascii_uppercase
m = list(x)
for i in m:
  while True:
    while i in test and loop < 2:
      loop +=1
      n = i.lower()
      j = test.replace(i,"_"+n)
      print(j)
deceze
  • 510,633
  • 85
  • 743
  • 889
  • I think [this](https://stackoverflow.com/a/1176023/13124794) answers your question. – odaiwa Oct 15 '22 at 08:35
  • why python2.7 tag when it looks like you use python3? Why 3 nested loops, especially the infinite `while True`? – buran Oct 15 '22 at 08:36

2 Answers2

0

Here's the algo for you to implement. Note no nested loops are necessary

let output be an empty string

for each symbol in input

   if symbol is uppercase
       unless this is a first symbol, append '_' to the output
       convert symbol to lower case

   append symbol to the output
gog
  • 10,367
  • 2
  • 24
  • 38
0

This is a somewhat similar implementation of the algorithm in python suggested by @gog

test = input("Enter your camelCasing")
ans=''
for i in range(len(test)):
    if test[i].isupper():
        if i==0:
            ans+=test[i].lower()
        else:
            ans+="_"+test[i].lower()
    else:
        ans+=test[i]
        
print(ans)
Om Khade
  • 40
  • 6