With this code, all I am trying to do is insert a dash between odd numbers and an asterisk between even ones. It does not work correctly with every input. It works with, e.g. 46879, but returns None with 468799, or does not insert * between 4 and 6 with 4546793. Why is it doing that? Thanks
def DashInsertII(num):
num_str = str(num)
flag_even=False
flag_odd=False
new_str = ''
for i in num_str:
n = int(i)
if n % 2 == 0:
flag_even = True
else:
flag_even = False
if n % 2 != 0:
flag_odd = True
else:
flag_odd = False
new_str = new_str + i
ind = num_str.index(i)
if ind < len(num_str) - 1:
m = int(num_str[ind+1])
if flag_even:
if m % 2 == 0:
new_str = new_str + '*'
else:
if m % 2 != 0:
new_str = new_str + '-'
else:
return new_str
print DashInsertII(raw_input())
num_str.index(i)will always return the first occurrence. You can't expectind+1to point to the next number if index returned the first occurrence, instead of the succeeding occurrence.