python - A function takes a string of numbers and inserts a dash before and after each odd digit: The exception: string cannot begin or end with '-' -


the task: write method takes in number , returns string, placing single dash before , after each odd digit. there 1 exception: don't start or end string dash.

here have:

num = "32343" dash = ""  digit in num:     if int(num[0]) % 2 != 0:         dash = digit + "-"     else:         dash += digit     if int(digit) % 2 != 0:         dash += "-" + digit + "-"     else:         dash += digit     if int(num[-1]) % 2 != 0:         dash += "-" + digit     else:         dash += digit  print(dash) 

it printing "3--3--3"

if tell me i'm going wrong, appreciated!

the solution series of if - else cases consider, quite self-explanatory:

num = "32343" string = "" in xrange(0,len(num)):     if == 0 , == len(num) - 1 , int(num[i]) % 2 != 0:         string = num[i]     elif == 0 , int(num[i]) % 2 != 0:         string = num[i] + "-"     elif == len(num) - 1  , int(num[i]) % 2 != 0:         string = string + "-" + num[i]     elif int(num[i]) % 2 !=0:         string =  string + "-" + num[i] + "-"     elif int(num[i]) % 2 == 0:         string = string + num[i]  print string  

Comments