python - "cannot concatenate 'str' and 'int' objects" error -


i keep getting "cannot concatenate 'str' , 'int' objects" error when try run following code. i'm pointed line 6 source of problem, can't see error! types seem consistent.

def dashinsert(num):    num_str = str(num)   new_str = ''    in num_str:     var1 = num_str[i:i+1]     var2 = num_str[i+1:i+2]      if var1 % 2 == 1 , var2 % 2 == 1:       new_str = new_str + num_str[i:i+1] + "-"     else:       new_str = new_str + num_str[i:i+1]    return new_str  # keep function call here   # see how enter arguments in python scroll down print dashinsert(raw_input())   

for in num_str: 

i not index in case, string character.

for example, if num in code 42, work flow be:

num_str = str(42) # '42' in num_str: # first iteration     var1 = num_str['4':'4'+1] # python: '4' + 1 = error 

what looking is:

for i, c in enumerate(num_str):     var1 = num_str[0:0+1] # python: 0 + 1 = 1 

see this answer.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -